How to get the format in hours when it exceeds 24?

8

I'm developing a system in PHP where I need to at some point get the total time of an audio file. This time is saved in the database in seconds and also in the time format.

The problem is that when you format these times through the seconds saved in the database, this works normally, but only if the file has less than 24 hours.

Example:

$date = new DateTime('@0'); // "zeramos" a data

$tempo_1 =  10 * 3600; // 10 horas

$tempo_2 = 18 * 3600; // 18 horas

$tempo_3 = 28 * 3600; // 28 horas


echo $date->setTime(0, 0, $tempo_1)->format('H:i:s'); // 10:00:00

echo $date->setTime(0, 0, $tempo_2)->format('H:i:s'); // 18:00:00

echo $date->setTime(0, 0, $tempo_3)->format('H:i:s'); // 04:00:00

In the last example, the desired result was 28:00:00 . However, because it is a class that works with dates, it returns 04:00:00 , on account of 24 equals 1 day.

How could I do to get this formatted time in hours, even if it exceeds 24 hours?

    
asked by anonymous 24.02.2015 / 02:54

3 answers

7

I do not recommend, but you can do a trick:

function dtLength($sec)
{
    $t=new DateTime("@".$sec);
    $r=new DateTime("@0");
    $i=$t->diff($r);
    $h=intval($i->format("%a"))*24+intval($i->format("%H"));
    return $h.":".$i->format("%I:%S");
}

$date = new DateTime;

$tempo_1 =  10 * 3600; // 10 horas

$tempo_2 = 18 * 3600; // 18 horas

$tempo_3 = 28 * 3600; // 28 horas


echo $date->setTime(0, 0, $tempo_1)->format('H:i:s'); // 10:00:00

echo $date->setTime(0, 0, $tempo_2)->format('H:i:s'); // 18:00:00

echo $date->setTime(0, 0, $tempo_3)->format('H:i:s'); // 04:00:00

echo dtLength($tempo_1);
echo dtLength($tempo_2);
echo dtLength($tempo_3);

See working on ideone .

Role removed from this answer in SO .

Another possibility is to work with DateInterval . This type shows times, which is what it seems to be wanting to use, rather than hours. It's probably more appropriate for what you want.

    
24.02.2015 / 03:10
7

The solutions presented above are very good.

But even though it sounds stupid, I ended up finding another solution to this problem using the SEC_TO_TIME function of Mysql .

See:

SELECT SEC_TO_TIME( 28 * 3600 )
--28:00:00

So, stay logged in if anyone has a problem like this!

It seems that at this point, Mysql simplified things much more than PHP itself.

Update

I've developed a library so we can work with the times more accurately. And I used some of the solutions applied here in the source code.

If interested:

link

    
24.02.2015 / 03:43
6
  

Note: In case the author solved using MySql, then this answer may happen to data coming from another "source" where it does not have a resource similar to SEC_TO_TIME .

If the time format is in seconds you can calculate using PHP without the need for "advanced" classes for this, as the example this answer in SOen , an example use:

<?php
function getFullHour($input) {
    $seconds = intval($input); //Converte para inteiro

    $negative = $seconds < 0; //Verifica se é um valor negativo

    if ($negative) {
        $seconds = -$seconds; //Converte o negativo para positivo para poder fazer os calculos
    }

    $hours = floor($seconds / 3600);
    $mins = floor(($seconds - ($hours * 3600)) / 60);
    $secs = floor($seconds % 60);

    $sign = $negative ? '-' : ''; //Adiciona o sinal de negativo se necessário

    return $sign . sprintf('%02d:%02d:%02d', $hours, $mins, $secs);
}

echo getFullHour(140801), PHP_EOL; //39:06:41
echo getFullHour(100800), PHP_EOL; //28:00:00

echo getFullHour(-140801), PHP_EOL; //-39:06:41
echo getFullHour(-100800), PHP_EOL; //-28:00:00
    
24.02.2015 / 04:00