Skip to content

Instantly share code, notes, and snippets.

@everaldomatias
Created September 19, 2020 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everaldomatias/bd3b036cc3eec90e5b4a4f98f23e3b41 to your computer and use it in GitHub Desktop.
Save everaldomatias/bd3b036cc3eec90e5b4a4f98f23e3b41 to your computer and use it in GitHub Desktop.
<?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