公告
财富商城
积分规则
提问
发文
2018-12-31 10:00发布
君临天下
I need to convert seconds to "Hour:Minute:Second".
For example: "685" converted to "00:11:25"
How can I achieve this?
Well I needed something that would reduce seconds into hours minutes and seconds, but would exceed 24 hours, and not reduce further down into days.
Here is a simple function that works. You can probably improve it... But here it is:
function formatSeconds($seconds) { $hours = 0;$minutes = 0; while($seconds >= 60){$seconds -= 60;$minutes++;} while($minutes >= 60){$minutes -=60;$hours++;} $hours = str_pad($hours, 2, '0', STR_PAD_LEFT); $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); $seconds = str_pad($seconds, 2, '0', STR_PAD_LEFT); return $hours.":".$minutes.":".$seconds; }
Anyone whose looking for this in the future, this gives the format the initial poster asked for.
$init = 685; $hours = floor($init / 3600); $hrlength=strlen($hours); if ($hrlength==1) {$hrs="0".$hours;} else {$hrs=$hours;} $minutes = floor(($init / 60) % 60); $minlength=strlen($minutes); if ($minlength==1) {$mins="0".$minutes;} else {$mins=$minutes;} $seconds = $init % 60; $seclength=strlen($seconds); if ($seclength==1) {$secs="0".$seconds;} else {$secs=$seconds;} echo "$hrs:$mins:$secs";
What about
print date('H:i:s', mktime(0, 0, 685, 0, 0));
without any extensions
This function my be useful, you could extend it:
function formatSeconds($seconds) { if(!is_integer($seconds)) { return FALSE; } $fmt = ""; $days = floor($seconds / 86400); if($days) { $fmt .= $days."D "; $seconds %= 86400; } $hours = floor($seconds / 3600); if($hours) { $fmt .= str_pad($hours, 2, '0', STR_PAD_LEFT).":"; $seconds %= 3600; } $mins = floor($seconds / 60 ); if($mins) { $fmt .= str_pad($mins, 2, '0', STR_PAD_LEFT).":"; $seconds %= 60; } $fmt .= str_pad($seconds, 2, '0', STR_PAD_LEFT); return $fmt;}
Try this:
date("H:i:s",-57600 + 685);
Taken from http://bytes.com/topic/php/answers/3917-seconds-converted-hh-mm-ss
最多设置5个标签!
Well I needed something that would reduce seconds into hours minutes and seconds, but would exceed 24 hours, and not reduce further down into days.
Here is a simple function that works. You can probably improve it... But here it is:
Anyone whose looking for this in the future, this gives the format the initial poster asked for.
What about
without any extensions
This function my be useful, you could extend it:
Try this:
Taken from
http://bytes.com/topic/php/answers/3917-seconds-converted-hh-mm-ss