Skip to content

Instantly share code, notes, and snippets.

@robozavri
Created June 22, 2018 13:19
Show Gist options
  • Save robozavri/c7206d8f5d1efe0fa91563db4deebbcd to your computer and use it in GitHub Desktop.
Save robozavri/c7206d8f5d1efe0fa91563db4deebbcd to your computer and use it in GitHub Desktop.
php count sum times
echo sum_the_time('01:20:22', '02:10:00'); // this will give you a result: 19:12:25
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}";
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); // Thanks to Patrick
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment