-
-
Save bantya/7df14511d60b00e524421777b1df4020 to your computer and use it in GitHub Desktop.
php count sum times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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