1

i have an array. i want to sum all time in the array so it will result sum of all time in the array. :

//print_r($chart_average);
Array ( [0] => 00:20:00 [1] => 00:03:45 [2])

how to sum all the time in array above and show result like below. How to calculate all array?

total time : 00:23:45

my answer for my reference after this :

 foreach ($chart_average as $time) {
        list($hour, $minute, $second) = explode(':', $time);
        $all_seconds += $hour * 3600;
        $all_seconds += $minute * 60;
        $all_seconds += $second;

    }

   $total_minutes = floor($all_seconds/60);
   $seconds = $all_seconds % 60;
   $hours = floor($total_minutes / 60); 
   $minutes = $total_minutes % 60;

    // returns the time already formatted
    echo sprintf('%02d:%02d:%02d', $hours, $minutes,$seconds);
airi
  • 585
  • 5
  • 21
  • 1
    did you see this? http://stackoverflow.com/questions/22681725/how-to-sum-n-number-of-time-hhmm-format – guergana Jul 01 '15 at 03:07
  • yes.. but it only have minute – airi Jul 01 '15 at 03:09
  • That's hours and minutes. – guergana Jul 01 '15 at 03:10
  • yes but mine already in array have H:M:ss .. i use array_sum but not work – airi Jul 01 '15 at 03:11
  • adapt that code on the function. use: foreach ($times as $time) { list($hour, $minute, $seconds) = explode(':', $time); $minutes += $hour * 60; $minutes += $minute; seconds += $minutes*60; } – guergana Jul 01 '15 at 03:12
  • better you answer the question then..>. – airi Jul 01 '15 at 03:13
  • $hours = floor($seconds / 3600); $seconds = $seconds % 3600; / and the minutes.. / // returns the time already formatted return sprintf('%02d:%02d', $hours, $minutes, $seconds); – guergana Jul 01 '15 at 03:18
  • I'd like to answer, but then people will downvote my answer... if you think it's right then I'll post it as an answer. – guergana Jul 01 '15 at 03:19
  • I think it's easy to figure out what follows from the code on the other post. :) – guergana Jul 01 '15 at 03:20
  • 1
    the code works but i have problems in second.. – airi Jul 01 '15 at 03:30
  • You can do it with modulus... Do $minutes = $second / 60; $seconds = $second % 60 (it was not 3600, sorry) – guergana Jul 01 '15 at 03:32
  • have the element with the total input seconds be 'second' so it's not the same as the second variable 'seconds' with the total seconds (counting from minutes and hours) ---- list($hour, $minute, $second) – guergana Jul 01 '15 at 03:33
  • I'll post the complete thing. :-/ – guergana Jul 01 '15 at 03:34
  • yes it right .. but i have problems in echo the below part in second...see my code: foreach ($chart_average as $time) { list($hour, $minute, $seconds) = explode(':', $time); $minutes += $hour * 60; $minutes += $minute; $seconds += $second % 60; } $hours = floor($minutes / 60); $minutes -= $hours * 60; //$seconds -= $second % 60; echo sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); – airi Jul 01 '15 at 03:38

3 Answers3

4
$times = array();

$times[] = "12:59";
$times[] = "0:58";
$times[] = "0:02";

// pass the array to the function
echo AddPlayTime($times);

function AddPlayTime($times) {

    // loop throught all the times
    foreach ($times as $time) {
        list($hour, $minute, $second) = explode(':', $time);
        $all_seconds += $hour * 3600;
        $all_seconds += $minute * 60; $all_seconds += $second;

    }


    $total_minutes = floor($all_seconds/60); $seconds = $all_seconds % 60;  $hours = floor($total_minutes / 60); $minutes = $total_minutes % 60;

    // returns the time already formatted
    return sprintf('%02d:%02d:%02d', $hours, $minutes,$seconds);
}
guergana
  • 374
  • 5
  • 19
  • i got this : 01:103:45 ... suppose : 01:43:45 – airi Jul 01 '15 at 03:48
  • yes, sorry, I changed this line: $total_minutes = floor($all_seconds/60); $seconds = $all_seconds % 60; $hours = floor($total_minutes / 60); $minutes = %total_minutes % 60; – guergana Jul 01 '15 at 03:51
  • see this http://stackoverflow.com/questions/6313656/split-down-a-number-in-seconds-to-days-hours-minutes-and-seconds – guergana Jul 01 '15 at 03:53
  • 1
    yes now your code right.. but have liltle error..%total_minutes % 60; suppose $total_minutes % 60;.. thanks .. – airi Jul 01 '15 at 03:55
  • 1
    cool..... this will be my reference after this.. i will using this more to produce chart.. thanks – airi Jul 01 '15 at 03:57
1

Try this..

$time = "00:58:50";
$time2 = "00:02:30";

$secs = strtotime($time2) - strtotime("00:00:00");
$result = date("H:i:s", strtotime($time) + $secs);
// 01:01:20
Jijesh Cherayi
  • 1,111
  • 12
  • 15
0

Kinda ugly but it works

$a = array('00:20:00', '00:03:45');
$h = $m = $s = 0;
foreach ($a as $time) {
    $time = new \DateTime($time);
    $h += $time->format('H');
    $m += $time->format('i');
    $s += $time->format('s');
}

$interval = new DateInterval("PT{$h}H{$m}M{$s}S");
echo $interval->format('%H:%I:%S');
galki
  • 8,149
  • 7
  • 50
  • 62