Sum of hours greater than 24h

5

I have a variable with the value '18: 00 ', which corresponds to a duration received through two-time calculations.

$tempo = '18:00'; // queria somar 8:00

Answer:

$temposomado = '2:00';

Desired value:

$temposomado = '26:00'

How can I get this second result in PHP?

    
asked by anonymous 20.04.2018 / 18:35

5 answers

5

Although the other answers are right, they are not adding up the seconds. So I decided to create an alternative.

I made this response based on in this answer and 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 at ideone

So, my version of the function AddPlayTime of the @ WictorChaves response based on the example above would look 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);
}
    
20.04.2018 / 19:25
5

Well, first you can pick up only the hours and minutes, then add

$tempo="18:40";
$tempo2="08:30";

$minutos1=substr($tempo, 3, 2);
$minutos2=substr($tempo2, 3, 2);
$soma_min=(int)$minutos2+(int)$minutos1;
if($soma_min > 60){
   $hora_mais=1;
   $soma_min=$soma_min-60;
}else{
   $hora_mais=0;
}
if($soma_min<10){
    $soma_min=$soma_min."0";
}
$hora1 = substr($tempo, 0, 2);
$hora2 = substr($tempo2, 0, 2);

$hora_somada= (int)$hora2+(int)$hora1+$hora_mais;
$tudo_junto=$hora_somada.":".$soma_min;
echo $tudo_junto;
    
20.04.2018 / 18:40
5

A form that I consider simple:

// Variáveis recebidas
$hora1 = '09:15';
$hora2 = '19:30';

// Quebra horas de minutos
$t1 = explode(':', $hora1);
$t2 = explode(':', $hora2);

// Converte para minutos e soma
$min_1 = ($t1[0] * 60 + $t1[1]);
$min_2 = ($t2[0] * 60 + $t2[1]);
$min_t = $min_1 + $min_2;

// Define o formato
$formato = '%02d:%02d';

// Converte os minutos para horas e minutos
$horas = floor($min_t / 60);
$minutos = ($min_t % 60);

// Imprime
echo 'Hora: ' . sprintf($formato, $horas, $minutos);

Here I use the same script to print intervals:

Problems generating schedules dynamically

    
20.04.2018 / 21:37
4

You can do this with this function:

AddPlayTime($times);

An example:

$times = array();

$times[] = "18:00";
$times[] = "8:00";

echo AddPlayTime($times);

function AddPlayTime($times) {
    $minutes = 0; 
    foreach ($times as $time) {
        list($hour, $minute) = explode(':', $time);
        $minutes += $hour * 60;
        $minutes += $minute;
    }

    $hours = floor($minutes / 60);
    $minutes -= $hours * 60;

    //Aqui ele verifica se passou de 24 hs
    if($hours > 24){
        $vezes = $hours / 24;
        $hours = $hours - (24*intval($vezes));
    }
    return sprintf('%02d:%02d', $hours, $minutes);
}

You create an array with the times you want to add and send it to function.

UPDATE

I interpreted the question differently, but according to Andrei Coelho he needs the sum of the terms, so just remove the if, as in this example:

$times = array();

$times[] = "18:00";
$times[] = "8:00";

echo AddPlayTime($times);

function AddPlayTime($times) {
    $minutes = 0; 
    foreach ($times as $time) {
        list($hour, $minute) = explode(':', $time);
        $minutes += $hour * 60;
        $minutes += $minute;
    }

    $hours = floor($minutes / 60);
    $minutes -= $hours * 60;

    return sprintf('%02d:%02d', $hours, $minutes);
}

Based on: link

    
20.04.2018 / 18:41
4

You already have several options to choose from, but I choose to also make my contribution with a slightly different one, although similar to that of @AndreiCoelho. As well as his, my implementation also takes into account seconds.

Implementation:

function obterSegundos($tempo){
    $tempos = explode(":", $tempo);
    $horasmins = (int)$tempos[0] * 3600 + (int)$tempos[1] * 60;
    return count($tempos) === 2 ? $horasmins : $horasmins + (int)$tempos[2];
}

function formatarSegundos($total){
    $horas = floor($total / 3600);
    $minutos = floor(($total - $horas * 3600) / 60);
    $segundos = $total % 60;
    return $segundos ? 
        sprintf('%02d:%02d:%02d', $horas, $minutos, $segundos):
        sprintf('%02d:%02d', $horas, $minutos);
}

$t1 = "18:00";
$t2 = "08:30:09";
$total = obterSegundos($t1) + obterSegundos($t2);
$t3 = formatarSegundos($total); //26:30:09

See it working on Ideone

Tests:

$t1 = "11:05";
$t2 = "05:35";
$t3 = formatarSegundos(obterSegundos($t1) + obterSegundos($t2)); //16:40

$t1 = "18:00:59";
$t2 = "08:00:55";
$t3 = formatarSegundos(obterSegundos($t1) + obterSegundos($t2)); //26:01:54

$t1 = "12:00:05";
$t2 = "01:30";
$t3 = formatarSegundos(obterSegundos($t1) + obterSegundos($t2)); //13:30:05

$t1 = "11:55";
$t2 = "01:56";
$t3 = formatarSegundos(obterSegundos($t1) + obterSegundos($t2)); //13:51

$t1 = "1:05";
$t2 = "2:10";
$t3 = formatarSegundos(obterSegundos($t1) + obterSegundos($t2)); //03:15
    
21.04.2018 / 01:29