00 Votes

PHP: Convert Seconds to Format 00:00:00

Question by Guest | 2014-09-30 at 22:13

I have a time given in seconds (for example 3611) and I would like to output this value in the format 00:00:00 (hours:minutes:seconds), which would be 01:00:11 for 3611. The reason is, that the value should be displayed in a more readable form on my website.

Is there any simple PHP function for a conversion like this?

ReplyPositiveNegative
0Best Answer0 Votes

For example, you can work with the PHP functions date(), sprintf()/printf() or a combination of date() and mktime():

$t = 3611;

// way 1
echo date("H:i:s", $t); 

// way 2
echo date("H:i:s", mktime(0, 0, 3611));

// way 3
echo sprintf("%02d:%02d:%02d", 
             floor($t/3600), floor($t/60)%60, $t%60);

Each of those lines will result in the output "01:00:11".

I have already explained the functionality of the functions used in this example in the answers of this question in detail.
2014-10-01 at 16:17

ReplyPositive Negative
Reply

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

PHP: Upload of large Files

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.