3

私はこの配列を持っています

$date = array{"2013-09-17 00:21:00",
              "2013-09-23 00:12:00",
              "2013-09-23 00:41:00",
              "2013-09-20 00:13:00",
              "2013-09-19 00:34:00",
              "2013-09-17 00:38:00"}

同じ日付を持つ配列内の時間を合計しようとしています。

これは私の期待される出力です:

 $date = array{"2013-09-17 00:59:00",
               "2013-09-23 00:53:00", 
               "2013-09-20 00:13:00",
               "2013-09-19 00:34:00"}

今、これは私がこれまでに試したことです

foreach($date as $key => $value)
        {
            $lf_date[] = date("Y-m-d",strtotime($value));
            $lf_time[] = date("H:i:s",strtotime($value));

            if(isset($lf_date[$key]))
            {
                 $output[] += $lf_time[$key]; 
            }
            else
            {
                 $output[] = $lf_time[$key]; 
            }

        }

これにより、0の出力T_Tが得られます。すでにGoogleで検索しようとしましたが、issetarray_key_existsを使用する必要があると言われていますが、機能させることができません。:(。私を助けてくれた人に感謝します。

4

1 に答える 1

1

使用する:

<?php
$date = array("2013-09-17 00:21:00",
              "2013-09-23 00:12:00",
              "2013-09-23 00:41:00",
              "2013-09-20 00:13:00",
              "2013-09-19 00:34:00",
              "2013-09-17 00:38:00");

$array = array();             
foreach($date as $key => $value)
{
    $lf_date = date("Y-m-d",strtotime($value));
    $lf_time = date("H:i:s",strtotime($value));

    $midnight = strtotime("0:00");

    if(!isset($array[$lf_date])) 
           $array[$lf_date] = 0;//check is array index exists

    $array[$lf_date] += strtotime($lf_time) - $midnight;
}

foreach($array as $key => $value)
{
    $midnight = strtotime("0:00");
    $array[$key] = $key." ".date("G:i:s", $midnight + $value);
}

$result = array_values($array);

print_r($result);

?>

出力:

Array
(
    [0] => 2013-09-17 0:59:00
    [1] => 2013-09-23 0:53:00
    [2] => 2013-09-20 0:13:00
    [3] => 2013-09-19 0:34:00
)
于 2013-10-07T10:03:56.690 に答える