Output is in seconds. convert to hh:mm:ss format in php

PhpTime

Php Problem Overview


  1. My output is in the format of 290.52262423327 seconds. How can i change this to 00:04:51?

  2. The same output i want to show in seconds and in HH:MM:SS format, so if it is seconds, i want to show only 290.52 seconds.(only two integers after decimal point)? how can i do this?

I am working in php and the output is present in $time variable. want to change this $time into $newtime with HH:MM:SS and $newsec as 290.52.

Thanks :)

Php Solutions


Solution 1 - Php

function foo($seconds) {
  $t = round($seconds);
  return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
}

echo foo('290.52262423327'), "\n";
echo foo('9290.52262423327'), "\n";
echo foo(86400+120+6), "\n";

prints

00:04:51
02:34:51
24:02:06

2)

echo round($time, 2);

Solution 2 - Php

Try this one

echo gmdate("H:i:s", 90);

Solution 3 - Php

Edit: A comment pointed out that the previous answer fails if the number of seconds exceeds a day (86400 seconds). Here's an updated version. The OP did not specify this requirement so this may be implemented differently than the OP might expect, and there may be much better answers here already. I just couldn't stand having provided an answer with this bug.

$iSecondsIn = 290.52262423327;

// Account for days.
$iDaysOut = 0;
while ($iSecondsIn >= 86400) {
    $iDaysOut += 1;
    $iSecondsIn -= 86400;
}

// Display number of days if appropriate.
if ($iDaysOut > 0) {
    print $iDaysOut.' days and ';
}

// Print the final product.
print date('H:i:s', mktime(0, 0, $iSecondsIn));

The old version, with the bug:

$iSeconds = 290.52262423327;
print date('H:i:s', mktime(0, 0, $iSeconds));

Solution 4 - Php

For till 23:59:59 hours you can use PHP default function

echo gmdate("H:i:s", 86399);

Which will only return the result till 23:59:59

If your seconds is more then 86399 than with the help of @VolkerK answer

$time = round($seconds);
echo sprintf('%02d:%02d:%02d', ($time/3600),($time/60%60), $time%60);

will be the best options to use ...

Solution 5 - Php

Try this:

$time = 290.52262423327;
echo date("h:i:s", mktime(0,0, round($time) % (24*3600)));

Solution 6 - Php

I dont know if this is the most efficient way, but if you also need to display days, this works:

function foo($seconds) { 
$t = round($seconds); 
return sprintf('%02d  %02d:%02d:%02d', ($t/86400%24), ($t/3600) -(($t/86400%24)*24),($t/60%60), $t%60);
}

Solution 7 - Php

Try this :)

private function conversionTempsEnHms($tempsEnSecondes)
    {
        $h = floor($tempsEnSecondes / 3600);
        $reste_secondes = $tempsEnSecondes - $h * 3600;
    
        $m = floor($reste_secondes / 60);
        $reste_secondes = $reste_secondes - $m * 60;
    
        $s = round($reste_secondes, 3); 
        $s = number_format($s, 3, '.', '');
    
        $h = str_pad($h, 2, '0', STR_PAD_LEFT);
        $m = str_pad($m, 2, '0', STR_PAD_LEFT);
        $s = str_pad($s, 6, '0', STR_PAD_LEFT);
    
        $temps = $h . ":" . $m . ":" . $s;
    
        return $temps;
    }

Solution 8 - Php

Personally, going off other peoples answers I made my own parser. Works with days, hours, minutes and seconds. And should be easy to expand to weeks/months etc. It works with deserialisation to c# as well

function secondsToTimeInterval($seconds) {
    $t = round($seconds);
    $days = floor($t/86400);
    $day_sec = $days*86400;
    $hours = floor( ($t-$day_sec) / (60 * 60) );
    $hour_sec = $hours*3600;
    $minutes = floor((($t-$day_sec)-$hour_sec)/60);
    $min_sec = $minutes*60;
    $sec = (($t-$day_sec)-$hour_sec)-$min_sec;
    return sprintf('%02d:%02d:%02d:%02d', $days, $hours, $minutes, $sec);
}

Solution 9 - Php

Based on https://stackoverflow.com/a/3534705/4342230, but adding days:

function durationToString($seconds) {
  $time = round($seconds);

  return sprintf(
    '%02dD:%02dH:%02dM:%02dS',
    $time / 86400,
    ($time / 3600) % 24,
    ($time / 60) % 60,
    $time % 60
  );
}

Solution 10 - Php

Numero uno... http://www.ckorp.net/sec2time.php (use this function)

Numero duo... echo round(290.52262423327,2);

Solution 11 - Php

$newtime = sprintf( "%02d:%02d:%02d", $time / 3600, $time / 60 % 60, $time % 60 );
$newsec = sprintf( "%.2f", $time );

Solution 12 - Php

If you're using Carbon (such as in Laravel), you can do this:

$timeFormatted = \Carbon\Carbon::now()->startOfDay()->addSeconds($seconds)->toTimeString();

But $timeFormatted = date("H:i:s", $seconds); is probably good enough.

Just see caveats.

Solution 13 - Php

Here was my implementation with microseconds

    /**
     * @example 00 d 00 h 00 min 00 sec 005098 ms (0.005098 sec.ms)
     */
    public function __toString()
    {
        // Add your code to get $seconds and $microseconds
        $time = round(($seconds + $microseconds), 6, PHP_ROUND_HALF_UP);

        return sprintf(
            '%02d d %02d h %02d min %02d sec %06d ms (%s sec.ms)',
            $time / 86400,
            ($time / 3600) % 24,
            ($time / 60) % 60,
            $time % 60,
            $time * 1000000 % 1000000,
            $time
        );
    }

Solution 14 - Php

echo date('H:i:s', round($time)%86400);

Solution 15 - Php

echo date('H:i:s',$time);

echo number_format($time,2);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScorpion KingView Question on Stackoverflow
Solution 1 - PhpVolkerKView Answer on Stackoverflow
Solution 2 - PhpAzam AlviView Answer on Stackoverflow
Solution 3 - PhpTeekinView Answer on Stackoverflow
Solution 4 - PhpMannu saraswatView Answer on Stackoverflow
Solution 5 - PhpMartyIXView Answer on Stackoverflow
Solution 6 - PhpSergeView Answer on Stackoverflow
Solution 7 - PhpZaglooView Answer on Stackoverflow
Solution 8 - PhpPassiveModdingView Answer on Stackoverflow
Solution 9 - PhpGuyPaddockView Answer on Stackoverflow
Solution 10 - PhpDejan MarjanovićView Answer on Stackoverflow
Solution 11 - PhpRob KView Answer on Stackoverflow
Solution 12 - PhpRyanView Answer on Stackoverflow
Solution 13 - PhpWilliam DesportesView Answer on Stackoverflow
Solution 14 - PhpTFleschenbergView Answer on Stackoverflow
Solution 15 - PhpMark BakerView Answer on Stackoverflow