php - Sum time if date already exists or has a duplicate -
i have 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 sum time inside array have same date.
this 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 have tried far
foreach($date $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 0 output t_t.. tried searching on google , says have use isset , array_key_exists can't work. :(. 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 $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 array index exists $array[$lf_date] += strtotime($lf_time) - $midnight; } foreach($array $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 )
Comments
Post a Comment