date - PHP Convert seconds into MM:DD:HH:MM:SS -
date - PHP Convert seconds into MM:DD:HH:MM:SS -
this question has reply here:
convert seconds days, hours, minutes , seconds 6 answersi help converting seconds mm:dd:hh:mm:ss format.
i have code here:
<?php // convert seconds months, days, hours, minutes, , seconds. function secondstotime($ss) { $s = $ss%60; $m = floor(($ss%3600)/60); $h = floor(($ss%86400)/3600); $d = floor(($ss%2592000)/86400); $m = floor($ss/2592000); homecoming "$m months, $d days, $h hours, $m minutes, $s seconds"; } ?> it outputs illustration of
0 months, 1 days, 3 hours, 46 minutes, 39 seconds
i
00:01:03:46:39
how can that?
from gather, looks you're interested in returning string values containing 2 digits separaed colons. assuming positioning doesn't need change, can similar following:
<?php // prefix single-digit values zero. function ensure2digit($number) { if($number < 10) { $number = '0' . $number; } homecoming $number; } // convert seconds months, days, hours, minutes, , seconds. function secondstotime($ss) { $s = ensure2digit($ss%60); $m = ensure2digit(floor(($ss%3600)/60)); $h = ensure2digit(floor(($ss%86400)/3600)); $d = ensure2digit(floor(($ss%2592000)/86400)); $m = ensure2digit(floor($ss/2592000)); homecoming "$m:$d:$h:$m:$s"; } or if don't thought of having 1 more function manage, perhaps may suit better:
<?php // convert seconds months, days, hours, minutes, , seconds. function secondstotime($ss) { $s = $ss%60; $m = floor(($ss%3600)/60); $h = floor(($ss%86400)/3600); $d = floor(($ss%2592000)/86400); $m = floor($ss/2592000); // ensure values 2 digits, prepending 0 if necessary. $s = $s < 10 ? '0' . $s : $s; $m = $m < 10 ? '0' . $m : $m; $h = $h < 10 ? '0' . $h : $h; $d = $d < 10 ? '0' . $d : $d; $m = $m < 10 ? '0' . $m : $m; homecoming "$m:$d:$h:$m:$s"; } and phone call our function (whichever method decide use):
<?php $ss = 123456; print secondstotime($ss); ?> php date math format seconds
Comments
Post a Comment