I have this array
$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"}
I'm trying to sum the time inside the array that have the same date.
This is my expected output:
$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"}
Now this is what I have tried so far
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];
}
}
This gives me a 0 output T_T.. I already tried searching on google and it says that I have to use isset and array_key_exists but I can't get it to work. :(. Thanks for anyone who could help me.
Use:
<?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);
?>
Output:
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
)