Solution A:
Convert SECONDS to an HH:MM:SS format.
- Code: Select all
function format_sec( $sec, $format = 'H:i:s' ){
return date($format, mktime(0,0,$sec));
}
Wow - simple and very useful... Heck, you don't even need a function for that, just drop the date() code into your output line!
Solution B:
This type of solution is all over the web.. Maybe it is a little faster, but it sure is ugly...
And this is a super slim version!!
- Code: Select all
function format_sec($sec) {
return
str_pad(intval(intval($sec) / 3600), 2, "0", STR_PAD_LEFT) // hours
. ':'
. str_pad(intval(($sec / 60) % 60), 2, "0", STR_PAD_LEFT) // minutes
. ':'
. str_pad(intval($sec % 60), 2, "0", STR_PAD_LEFT); // seconds
}
I ran both functions in a loop 1 million times on a WinXP machine...
Solution A - 10.7407 seconds
Solution B - 9.4821 seconds
I ran both functions in a loop 1 million times on a Unix machine...
Solution A - 10.5965 seconds
Solution B - 6.4008 seconds
Since I'm not running it a million times... I'm going to stick with Solution A.
It is interesting how much faster the Unix box is to run the Solution B...
