Skip to content

Instantly share code, notes, and snippets.

@bantya
Forked from robozavri/php count sum times
Last active December 20, 2018 19:30
Show Gist options
  • Save bantya/7df14511d60b00e524421777b1df4020 to your computer and use it in GitHub Desktop.
Save bantya/7df14511d60b00e524421777b1df4020 to your computer and use it in GitHub Desktop.
php count sum times
print_r(sum_the_time('01:20:22', '02:10:00')); // this will give you a result: 03:30:22
// Credit: https://gist.github.com/robozavri/c7206d8f5d1efe0fa91563db4deebbcd
function sum_the_time($time1, $time2)
{
$times = [$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;
$times = [$hours, $minutes, $seconds];
return sprintf('%02d:%02d:%02d', ...$times); // Thanks to Patrick
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment