Jump to content

Extremely simple time formatting question


pianoman993

Recommended Posts

Hello, I believe I have a fairly simple question about converting time. I currently have a variable containing a number of seconds. Is there any way I could format that number of seconds so that it displays as something like 00:00:10.

 

So for example if I have a variable containing 10 seconds, how could I format that to display the 10 seconds as 00:00:10?

 

Thanks a bunch in advance for any help. :-D

- Mark

Try this function:

 

    function split_seconds($seconds)
    {
        // get the minutes

        $minutes = floor($seconds / 60) ;

        $seconds_left = $seconds % 60 ;

        // get the hours

        $hours = floor($minutes / 60) ;

        $minutes_left = $minutes % 60 ;

        // (test) show the result 

        echo "$hours : $minutes_left :  $seconds_left" ;
    }

    split_seconds(68648) ;

?>

this is simple math:

 

$time = 10;

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

$formatted = sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);

 

i'll admit i haven't tested it, but give it a shot.

 

EDIT:  beaten to it, but here's another way to skin the cat.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.