Created
September 19, 2020 01:23
-
-
Save everaldomatias/bd3b036cc3eec90e5b4a4f98f23e3b41 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
/** | |
* Soma um ou mais horários no formato 00:00 | |
* | |
* @link https://vijayasankarn.wordpress.com/2016/12/27/sum-n-times-php/ | |
* | |
* @param array $times Lista de horários para somar | |
* @return string | |
*/ | |
function increment_time(array $times) | |
{ | |
$seconds = 0; | |
foreach ($times as $time) { | |
list($hour, $minute, $second) = array_pad(explode(':', $time), 3, null); | |
$seconds += $hour * 3600; | |
$seconds += $minute * 60; | |
$seconds += $second; | |
} | |
$hours = floor($seconds / 3600); | |
$seconds -= $hours * 3600; | |
$minutes = floor($seconds / 60); | |
$seconds -= $minutes * 60; | |
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment