views:

248

answers:

3

Hi,

I'm trying to pad a time (playtime of an MP3) using sprintf() in PHP.

sprintf("%02d:2d:2d", $time);

The function that returns the time gets, for example, '1:56' if the MP3 is 1 minute 56 seconds long, and that call brings back "01:56:00" (whereas it needs to be 00:01:56). How can I make this work? How can I tell sprintf to pad on the left?

Or is there a better way to go about this? A more appropriate function maybe?

Thanks

+2  A: 

I think you'll need to calculate each element separately, so how about:

sprintf("%02d:%02d:%02d", floor($time/3600), floor($time/60)%60, $time%60);
Paul Dixon
Thanks, mine ended up a little different, but you put me on the right track.
Delameko
+1  A: 

You can use the date-function.

Something like this should work, if $time is in unix timestamp format:

print(date("H:i:s", $time));
Espo
A: 

you should use strftime($format,$timestamp) ... probably as this:

strftime("%H:%M:%S",$time)
back2dos