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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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