PHP date () Hours, minutes, and seconds without leading zeros

Possible duplicate:
Convert seconds per hour: Minutes: seconds

I searched all over the internet to find a good way to convert seconds to minutes: seconds without leading zeros. I already checked this question , which was the only one I could even find, however, none of these answers look very good. Perhaps this is the only and best way to achieve this, but I would not have hoped.

I did this and it gives me the number of minutes without leading zeros, however I cannot get a second. The only way I can do this is to do a few lines of math, etc., but it seems like terrible work on something simple than this ... that I don’t know why PHP doesn’t. If it were built in minutes and seconds anyway ....

intval(gmdate("i:s", $duration)); 

Edit All I am trying to do is convert the number of seconds in a video to H: M: S format.

+6
source share
3 answers
 implode( ':', array_map( function($i) { return intval($i, 10); }, explode(':', gmdate('H:i:s', $duration)) ) ) 

however how about if hour == 0 then don't type 0: and just type m: s

 preg_replace( '~^0:~', '', implode( ':', array_map( function($i) { return intval($i, 10); }, explode(':', gmdate('H:i:s', $duration)) ) ) ) 
+2
source

I would just write it iteratively:

 function duration_to_timestring($duration) { $s = []; if ($duration > 3600) { $s[] = floor($duration / 3600); $duration %= 3600; } $s[] = floor($duration / 60); $s[] = floor($duration % 60); return join(':', $s); } 
+2
source

gmdate takes the timestamp as the second parameter. You should do something like this:

 echo gmdate("H:i:s", mktime(0, 0, 0, 1, 1, 1998) + $duration); 

intval should not be there, as you get the string and convert it to int again. With H: i: s you have 10:40:05.
This, however, will not work if you have a duration> then 24 hours.

+1
source

All Articles