Add hours to an array

0

I need to total the column hora_agente of type time . I need the value returned in hours. Can be longer than 24 hours.

In the example I did, it is returning 5:55, and should return 53:55. I believe that when he gives 24 hours, he resumes the count.

If anyone knows how to do it right in SQL, better yet, the database is informix .

$total_horas=0;foreach($dados_temposas$t){$horariof=trim($t['HORA_AGENTE']);$partesf=explode(':',$horariof);$segundosf=$partesf[0]*3600+$partesf[1]*60;$total_horas+=$segundosf;}$horas_geral=gmdate("h:i:s", $total_horas);

            echo $horas_geral;

            //resultado = 05:55:00
            //resultado esperado = 53:55
    
asked by anonymous 22.09.2017 / 13:15

3 answers

0

You can create a function to do this treatment, and just call it at the end of your loop.

function convertTime( $total_hours ) 
{
    $hours   = floor( $total_hours / 3600 );
    $minutes = floor( ( $total_hours - ( $hours * 3600 ) ) / 60 );
    $seconds = floor( $total_hours % 60 );

    return $hours . ":" . $minutes . ":" . $seconds;
}

Example usage:

echo convertTime( 194100 ); // 53:55:00
    
22.09.2017 / 15:13
0

Your problem is precisely in the gmdate function, because it will format a date from a timestamp ( Unix Epoch ). As in the example below:

gmdate("d/m/Y h:i:s", '1000000'); // 12/01/1970 01:46:40

You could add / divide / multiply the time you have already calculated, to reach the end time. However, I do not really like this approach, because it leaves out what PHP has internally already implemented.

In this case, PHP has a specific class to work with time / date ranges, which is the class DateInterval . However, it is not so trivial to work with it for your specific case, because, natively, you can not add one DateInterval to another.

Another detail is the format that the DateInterval needs to be created, which is different from the traditional date format (so to speak).

On the other hand, you can use the DateTime class together, which helps us to create a DateInterval without changing the format coming from the database.

//é necessária uma data base (00:00:00) - $baseDate
//e uma data para adicionar/somar os tempos vindos do SGBD - $date
$baseDate = clone $date = \DateTime::createFromFormat('H:i:s' , '00:00:00');

//dados vindo do banco de dados
$array = [
    '04:30',
    '05:30',
    '04:23',
    '02:35',
    '01:50',
    '03:25',
    '03:40',
    '02:30'
];    

//percorre cada valor do array
foreach($array as $time)
{
    //cria-se o date time com o tempo informado
    $dateTime = \DateTime::createFromFormat('H:i' , $time);

    //realiza o diff com a $baseDate para criar o DateInterval
    $diff = $baseDate->diff($dateTime);

    //adiciona o diff ao DateTime que somará o tempo
    $date->add($diff);
}

//realiza o último diff entre o DateTime de soma e a base date
$interval = $baseDate->diff($date);

//DateInterval mantêm em dias (%a) tudo que for acima de 24 horas.
$hours = $interval->format('%H') + ($interval->format('%a') * 24);

//exibe o tempo
echo $hours.$interval->format(':%I'); // Saída: 28:23

The code has gotten a bit lengthy for the explanation, however, you can use objects that can be reused for display and other calculations when needed.

    
22.09.2017 / 15:18
0

Example - ideone

$times = array('24:10','8:25','6:45','7:05','7:00','0:30');

$seconds = 0;

foreach ( $times as $time )
{
list( $g, $i ) = explode( ':', $time );
$seconds += $g * 3600;
$seconds += $i * 60;
}

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

echo "{$hours}:{$minutes}";

list - is a native element of the PHP language and allows you to perform assignments of values from an array to a set of variables in a simplified way.

    
24.09.2017 / 06:51