本文翻译自:Convert seconds to Hour:Minute:Second
I need to convert seconds to "Hour:Minute:Second". 我需要将秒转换为“小时:分钟:秒”。
For example: "685" converted to "00:11:25" 例如:“685”转换为“00:11:25”
How can I achieve this? 我怎样才能做到这一点?
参考:https://stackoom.com/question/DJGe/将秒转换为小时-分钟-秒
Just in case anyone else is looking for a simple function to return this nicely formatted (I know it is not the format the OP asked for), this is what I've just come up with. 以防万一其他人正在寻找一个简单的函数来返回这个格式很好的(我知道它不是OP要求的格式),这就是我刚刚提出的。 Thanks to @mughal for the code this was based on. 感谢@mughal的代码,这是基于。
function format_timer_result($time_in_seconds){ $time_in_seconds = ceil($time_in_seconds); // Check for 0 if ($time_in_seconds == 0){ return 'Less than a second'; } // Days $days = floor($time_in_seconds / (60 * 60 * 24)); $time_in_seconds -= $days * (60 * 60 * 24); // Hours $hours = floor($time_in_seconds / (60 * 60)); $time_in_seconds -= $hours * (60 * 60); // Minutes $minutes = floor($time_in_seconds / 60); $time_in_seconds -= $minutes * 60; // Seconds $seconds = floor($time_in_seconds); // Format for return $return = ''; if ($days > 0){ $return .= $days . ' day' . ($days == 1 ? '' : 's'). ' '; } if ($hours > 0){ $return .= $hours . ' hour' . ($hours == 1 ? '' : 's') . ' '; } if ($minutes > 0){ $return .= $minutes . ' minute' . ($minutes == 1 ? '' : 's') . ' '; } if ($seconds > 0){ $return .= $seconds . ' second' . ($seconds == 1 ? '' : 's') . ' '; } $return = trim($return); return $return; }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";Use function gmdate() only if seconds are less than 86400 (1 day) : 仅当秒小于86400 (1天)时才使用函数gmdate() ) :
$seconds = 8525; echo gmdate('H:i:s', $seconds); # 02:22:05See: gmdate() 请参阅: gmdate()
Run the Demo 运行演示
Convert seconds to format by 'foot' no limit* : 将秒转换为'脚' 无限制格式* :
$seconds = 8525; $H = floor($seconds / 3600); $i = ($seconds / 60) % 60; $s = $seconds % 60; echo sprintf("%02d:%02d:%02d", $H, $i, $s); # 02:22:05See: floor() , sprintf() , arithmetic operators 请参阅: floor() , sprintf() , 算术运算符
Run the Demo 运行演示
Example use of DateTime extension: 使用DateTime扩展的示例:
$seconds = 8525; $zero = new DateTime("@0"); $offset = new DateTime("@$seconds"); $diff = $zero->diff($offset); echo sprintf("%02d:%02d:%02d", $diff->days * 24 + $diff->h, $diff->i, $diff->s); # 02:22:05See: DateTime::__construct() , DateTime::modify() , clone , sprintf() 请参阅: DateTime :: __ construct() , DateTime :: modify() , clone , sprintf()
Run the Demo 运行演示
MySQL example range of the result is constrained to that of the TIME data type, which is from -838:59:59 to 838:59:59 : 结果的MySQL示例范围限制为TIME数据类型的范围,即-838:59:59到838:59:59 :
SELECT SEC_TO_TIME(8525); # 02:22:05See: SEC_TO_TIME 请参阅: SEC_TO_TIME
Run the Demo 运行演示
PostgreSQL example: PostgreSQL示例:
SELECT TO_CHAR('8525 second'::interval, 'HH24:MI:SS'); # 02:22:05Run the Demo 运行演示
The gmtdate() function didn't work for me as I was tracking hours worked on a project and if it's over 24 hours, you get amount left over after 24 hours is subtracted. gmtdate()函数对我来说不起作用,因为我正在跟踪项目的工作时间,如果超过24小时,则会减去24小时后剩余的金额。 In other words 37 hours becomes 13 hours. 换句话说,37小时变为13小时。 (all as stated above by Glavic - thanks for your examples!) This one worked well: (如上所述,Glavic - 感谢你的例子!)这个很好用:
Convert seconds to format by 'foot' no limit : $seconds = 8525; $H = floor($seconds / 3600); $i = ($seconds / 60) % 60; $s = $seconds % 60; echo sprintf("%02d:%02d:%02d", $H, $i, $s); # 02:22:05