Although the other answers are right, they are not adding up the seconds. So I decided to create an alternative.
I made that response based on in this answer and in this answer
$tempo_inicial= "18:59:59";
$tempo_somado= "07:00:01";
sscanf($tempo_inicial, "%d:%d:%d", $horas_inicial, $minutos_inicial, $segundos_inicial);
sscanf($tempo_somado, "%d:%d:%d", $horas_somado, $minutos_somado, $segundos_somado);
$tempo_segundos_inicial = $horas_inicial * 3600 + $minutos_inicial * 60 + (isset($segundos_inicial) ? $segundos_inicial : 0);
$tempo_segundos_somado = $horas_somado * 3600 + $minutos_somado * 60 + (isset($segundos_somado) ? $segundos_somado : 0);
$total = $tempo_segundos_inicial + $tempo_segundos_somado;
$horas = floor($total / 3600);
$minutos = floor(($total - ($horas * 3600)) / 60);
$segundos = floor($total % 60);
$temposomado = sprintf('%02d:%02d:%02d', $horas, $minutos, $segundos);
echo $temposomado; // saída será 26:00:00
See working on ideone
So, a version of my role AddPlayTime
@Wictorchaves response based on the example above would be like this:
$times = array();
$times[] = "18:59:59";
$times[] = "07:00";
$times[] = "00:00:01";
echo AddPlayTime2($times);
function AddPlayTime2($times) {
$total = 0;
foreach ($times as $time) {
if(substr_count($time, ":") == 1)
$time .= ":00";
sscanf($time, "%02d:%02d:%02d", $hour, $minute, $second);
$total += $hour * 3600 + $minute * 60 + $second;
}
$horas = floor($total / 3600);
$minutos = floor(($total - ($horas * 3600)) / 60);
$segundos = floor($total % 60);
return sprintf('%02d:%02d:%02d', $horas, $minutos, $segundos);
}
Probably it is considering as hours, I believe that to sum the minutes you should do: $time = '00:18'; For it to consider the minutes
– Thiago Pereira
this is as string yet.
– Luís Almeida
It’s time... imagine 18:00:00. It can also be...
– White
PHP has the Datetime object to work with time. You’ve already seen something about?
– gmsantos
yes I got to see @gmsantos, but for now, this was the best chance for the case I’m developing.
– White
Friend, I think you should consider one of the correct answers. I believe that either "I like Trains" or Isac. In my opinion they are the best. Hug!
– Andrei Coelho
I’ve put the right answer :)
– White