Convert seconds to Hour:Minute:Second
我需要将秒转换为"小时:分钟:秒"。
例如:"685"转换为"00:11:25"
我怎样才能做到这一点?
您可以使用
1 |
一小时3600秒,一分钟60秒,为什么不:
1 2 3 4 5 6 7 8 9 10 |
产生:
1 2 |
(我没有做过这么多的测试,所以地板可能会出错)
干得好
1 2 3 4 5 6 |
只有在秒数小于
1 2 3 |
参见:GMDATA()
运行演示
按"英尺"无限制*将秒转换为格式:
1 2 3 4 5 6 |
请参见:floor()、sprintf()、算术运算符
运行演示
1 2 3 4 5 6 | $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:05 |
请参见:datetime::uu construct(),datetime::modify(),clone,Simulf()
运行演示
结果的mysql示例范围限定为时间数据类型,即从
1 2 | SELECT SEC_TO_TIME(8525); # 02:22:05 |
参见:秒到秒时间
运行演示
PostgreSQL示例:
1 2 | SELECT TO_CHAR('8525 second'::interval, 'HH24:MI:SS'); # 02:22:05 |
运行演示
其他解决方案使用
输入:
输入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // TEST // 1 Day 6 Hours 50 Minutes 31 Seconds ~ 111031 seconds $time = 111031; // time duration in seconds $days = floor($time / (60 * 60 * 24)); $time -= $days * (60 * 60 * 24); $hours = floor($time / (60 * 60)); $time -= $hours * (60 * 60); $minutes = floor($time / 60); $time -= $minutes * 60; $seconds = floor($time); $time -= $seconds; echo"{$days}d {$hours}h {$minutes}m {$seconds}s"; // 1d 6h 50m 31s |
1 |
如果
例如:
1 |
这里有一个处理负秒和超过1天的秒数的一行程序。
1 2 |
例如:
1 2 3 |
输出:-26:03:04
编写这样的函数以返回数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function secondsToTime($seconds) { // extract hours $hours = floor($seconds / (60 * 60)); // extract minutes $divisor_for_minutes = $seconds % (60 * 60); $minutes = floor($divisor_for_minutes / 60); // extract the remaining seconds $divisor_for_seconds = $divisor_for_minutes % 60; $seconds = ceil($divisor_for_seconds); // return the final array $obj = array( "h" => (int) $hours, "m" => (int) $minutes, "s" => (int) $seconds, ); return $obj; } |
然后简单地调用如下函数:
1 | secondsToTime(100); |
输出是
1 |
见:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** * Convert number of seconds into hours, minutes and seconds * and return an array containing those values * * @param integer $inputSeconds Number of seconds to parse * @return array */ function secondsToTime($inputSeconds) { $secondsInAMinute = 60; $secondsInAnHour = 60 * $secondsInAMinute; $secondsInADay = 24 * $secondsInAnHour; // extract days $days = floor($inputSeconds / $secondsInADay); // extract hours $hourSeconds = $inputSeconds % $secondsInADay; $hours = floor($hourSeconds / $secondsInAnHour); // extract minutes $minuteSeconds = $hourSeconds % $secondsInAnHour; $minutes = floor($minuteSeconds / $secondsInAMinute); // extract the remaining seconds $remainingSeconds = $minuteSeconds % $secondsInAMinute; $seconds = ceil($remainingSeconds); // return the final array $obj = array( 'd' => (int) $days, 'h' => (int) $hours, 'm' => (int) $minutes, 's' => (int) $seconds, ); return $obj; } |
从:将秒转换为天、小时、分钟和秒
gmtdate()函数对我来说不起作用,因为我在跟踪一个项目的工作时间,如果超过24小时,则减去24小时后的剩余量。换句话说,37小时变为13小时。(以上所有内容均由Glavic提供-感谢您的示例!)这个很好用:
1 2 3 4 5 6 7 |
这个函数我很有用,你可以扩展它:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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;} |
试试这个:
1 |
取自http://bytes.com/topic/php/answers/3917-seconds-converted-hh-mm-ss
如果你不喜欢被接受的答案或曾经受欢迎的答案,那么试试这个。
1 2 3 4 5 6 7 8 9 10 11 12 13 | function secondsToTime($seconds_time) { if ($seconds_time < 24 * 60 * 60) { return gmdate('H:i:s', $seconds_time); } else { $hours = floor($seconds_time / 3600); $minutes = floor(($seconds_time - $hours * 3600) / 60); $seconds = floor($seconds_time - ($hours * 3600) - ($minutes * 60)); return"$hours:$minutes:$seconds"; } } secondsToTime(108620); // 30:10:20 |
解决方案:https://gist.github.com/stevejobzniak/c91a8e2426bac5cb9b0c1bc45e4b
这是一个非常干净和简短的方法!这段代码尽可能避免冗长的函数调用和逐段的字符串构建,以及人们为此所做的巨大而庞大的函数。
它生成"1h05m00s"格式,并在数分钟和数秒内使用前导零,只要前面有另一个非零时间组件。
它会跳过所有空的引导组件,以避免给您提供"0h00M01s"之类的无用信息(而会显示为"1s")。
示例结果:"1s"、"1M00s"、"19m08s"、"1H00ms"、"4h08m39s"。
1 2 3 4 5 6 7 8 | $duration = 1; // values 0 and higher are supported! $converted = [ 'hours' => floor( $duration / 3600 ), 'minutes' => floor( ( $duration / 60 ) % 60 ), 'seconds' => ( $duration % 60 ) ]; $result = ltrim( sprintf( '%02dh%02dm%02ds', $converted['hours'], $converted['minutes'], $converted['seconds'] ), '0hm' ); if( $result == 's' ) { $result = '0s'; } |
如果要使代码更短(但可读性更低),可以避免使用
1 2 3 |
以上两个代码段的持续时间必须大于或等于0。不支持负持续时间。但您可以使用以下替代代码来处理负持续时间:
1 2 3 4 5 6 7 8 9 10 11 12 | $duration = -493; // negative values are supported! $wasNegative = FALSE; if( $duration < 0 ) { $wasNegative = TRUE; $duration = abs( $duration ); } $converted = [ 'hours' => floor( $duration / 3600 ), 'minutes' => floor( ( $duration / 60 ) % 60 ), 'seconds' => ( $duration % 60 ) ]; $result = ltrim( sprintf( '%02dh%02dm%02ds', $converted['hours'], $converted['minutes'], $converted['seconds'] ), '0hm' ); if( $result == 's' ) { $result = '0s'; } if( $wasNegative ) { $result ="-{$result}"; } // $result is now"-8m13s" |
使用datetime的一个简单方法是:
1 2 3 4 5 6 7 | $time = 60; //sec. $now = time(); $rep = new DateTime('@'.$now); $diff = new DateTime('@'.($now+$time)); $return = $diff->diff($rep)->format($format); //output: 01:04:65 |
这是一个简单的解决方案,它可以让你使用日期时间的格式方法。
在爪哇,你可以用这种方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | private String getHmaa(long seconds) { String string; int hours = (int) seconds / 3600; int remainder = (int) seconds - hours * 3600; int mins = remainder / 60; //remainder = remainder - mins * 60; //int secs = remainder; if (hours < 12 && hours > 0) { if (mins < 10) { string = String.valueOf((hours < 10 ?"0" + hours : hours) +":" + (mins > 0 ?"0" + mins :"0") +" AM"); } else { string = String.valueOf((hours < 10 ?"0" + hours : hours) +":" + (mins > 0 ? mins :"0") +" AM"); } } else if (hours >= 12) { if (mins < 10) { string = String.valueOf(((hours - 12) < 10 ?"0" + (hours - 12) : ((hours - 12) == 12 ?"0" : (hours - 12))) +":" + (mins > 0 ?"0" + mins :"0") + ((hours - 12) == 12 ?" AM" :" PM")); } else { string = String.valueOf(((hours - 12) < 10 ?"0" + (hours - 12) : ((hours - 12) == 12 ?"0" : (hours - 12))) +":" + (mins > 0 ? mins :"0") + ((hours - 12) == 12 ?" AM" :" PM")); } } else { if (mins < 10) { string = String.valueOf("0" +":" + (mins > 0 ?"0" + mins :"0") +" AM"); } else { string = String.valueOf("0" +":" + (mins > 0 ? mins :"0") +" AM"); } } return string; } |
如果您需要在javascript中这样做,您可以在这里回答的一行代码中使用javascript将秒转换为hh-mm-ss。用要转换的内容替换秒。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | function timeToSecond($time){ $time_parts=explode(":",$time); $seconds= ($time_parts[0]*86400) + ($time_parts[1]*3600) + ($time_parts[2]*60) + $time_parts[3] ; return $seconds; } function secondToTime($time){ $seconds = $time % 60; $seconds<10 ?"0".$seconds : $seconds; if($seconds<10) { $seconds="0".$seconds; } $time = ($time - $seconds) / 60; $minutes = $time % 60; if($minutes<10) { $minutes="0".$minutes; } $time = ($time - $minutes) / 60; $hours = $time % 24; if($hours<10) { $hours="0".$hours; } $days = ($time - $hours) / 24; if($days<10) { $days="0".$days; } $time_arr = array($days,$hours,$minutes,$seconds); return implode(":",$time_arr); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $given = 685; /* * In case $given == 86400, gmdate("H" ) will convert it into '00' i.e. midnight. * We would need to take this into consideration, and so we will first * check the ratio of the seconds i.e. $given:$number_of_sec_in_a_day * and then after multiplying it by the number of hours in a day (24), we * will just use"floor" to get the number of hours as the rest would * be the minutes and seconds anyways. * * We can also have minutes and seconds combined in one variable, * e.g. $min_sec = gmdate("i:s", $given ); * But for versatility sake, I have taken them separately. */ $hours = ( $given > 86399 ) ? '0'.floor( ( $given / 86400 ) * 24 )-gmdate("H", $given ) : gmdate("H", $given ); $min = gmdate("i", $given ); $sec = gmdate("s", $given ); echo $formatted_string = $hours.':'.$min.':'.$sec; |
要将其转换为函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
好吧,我需要一些东西,可以将秒减为小时、分钟和秒,但会超过24小时,而不会再减为几天。
这是一个简单的函数。你也许可以改善它…但这里是:
1 2 3 4 5 6 7 8 9 10 | 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; } |
任何人在将来寻找这个,这给出了最初的海报要求的格式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $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"; |
为了防止其他人正在寻找一个简单的函数来返回这个格式良好的函数(我知道这不是OP要求的格式),这是我刚刚想到的。感谢@Mughal提供的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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; } |
怎么样
无任何扩展
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php $time=3*3600 + 30*60; $year=floor($time/(365*24*60*60)); $time-=$year*(365*24*60*60); $month=floor($time/(30*24*60*60)); $time-=$month*(30*24*60*60); $day=floor($time/(24*60*60)); $time-=$day*(24*60*60); $hour=floor($time/(60*60)); $time-=$hour*(60*60); $minute=floor($time/(60)); $time-=$minute*(60); $second=floor($time); $time-=$second; if($year>0){ echo $year." year,"; } if($month>0){ echo $month." month,"; } if($day>0){ echo $day." day,"; } if($hour>0){ echo $hour." hour,"; } if($minute>0){ echo $minute." minute,"; } if($second>0){ echo $second." second,"; } |