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

Viewed 53857
  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 :)

16 Answers

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

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

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

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.

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

Simple formatter with progressively added parts - sample:

  • formatTime(123) => 2m 3s
  • formatTime(7400) => 2h 3m 20s
  • formatTime(999999) => 11d 13h 46m 39s

function formatTime($secs)
{
    $secs = max(0, intval($secs));
    if($secs > 0){
        $out = [];
        $yrs = floor($secs / 31536e3);
        if($yrs){
            $out[] = $yrs."y";
        }
        $rem = $secs - $yrs * 31536e3;
        $days = floor($rem / 86400);
        if($days || $out){
            $out[] = $days."d";
        }
        $rem -= $days * 86400;
        $hrs = floor($rem / 3600);
        if($hrs || $out){
            $out[] = $hrs."h";
        }
        $rem -= $hrs * 3600;
        $min = floor($rem / 60);
        if($min || $out){
            $out[] = $min."m";
        }
        $rem -= $min * 60;
        $out[] = $rem."s";
        return implode(" ", $out);
    }
    return 0;
}
Related