54
votes
  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 :)

14
Are you looking for a duration or a time stamp? - Rob K

14 Answers

120
votes

1)

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);
21
votes

Try this one

echo gmdate("H:i:s", 90);
4
votes
$iSeconds = 290.52262423327;
print date('H:i:s', mktime(0, 0, $iSeconds));
3
votes

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 ...

2
votes

Try this:

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

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);
}
1
votes

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;
    }
1
votes

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);
}
1
votes

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
  );
}
0
votes

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

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

0
votes

1)

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

2)

$newsec = sprintf( "%.2f", $time );
0
votes

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.

0
votes

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
        );
    }
-1
votes
echo date('H:i:s',$time);

echo number_format($time,2);