-1

I want to add 2 time variable in php. I use below code but it returns a big number. how can I have a time like (15:20:00) at the end?

code :

    $time1="10:00:00";
    $time2="05:20:00";
    $sum = strtotime($time1)+strtotime($time2);
    echo date("H:i:s",$sum);

Expected result: 15:20:00

mjyazdani
  • 2,110
  • 6
  • 33
  • 64
  • $CBroe I did not get my answer with this link, if you think it's duplicate please answer my question with the information that you got with that link. – mjyazdani Jul 27 '14 at 20:29

2 Answers2

2

Add to the end

$sum = strftime('%H:%M:%S', $sum);

http://php.net/manual/en/function.strftime.php

graywolf
  • 7,092
  • 7
  • 53
  • 77
1

Try something like :

    echo sum_the_time($time1, $time2); 

function sum_the_time($time1, $time2) {
  $times = array($time1, $time2);
  $seconds = 0;
  foreach ($times as $time)
  {
    list($hour,$minute,$second) = explode(':', $time);
    $seconds += $hour*3600;
    $seconds += $minute*60;
    $seconds += $second;
  }
  $hours = floor($seconds/3600);
  $seconds -= $hours*3600;
  $minutes  = floor($seconds/60);
  $seconds -= $minutes*60;
  return "{$hours}:{$minutes}:{$seconds}";
}
SpencerX
  • 5,453
  • 1
  • 14
  • 21