将秒转换为小时:分钟:秒
我需要将秒转换为“小时:分钟:秒”。
例如:“685”转换为“00:11:25”
我怎样才能做到这一点?
你可以使用gmdate()
函数:
echo gmdate("H:i:s", 685);
一小时3600秒,一分钟60秒,为什么不呢:
<?php $init = 685; $hours = floor($init / 3600); $minutes = floor(($init / 60) % 60); $seconds = $init % 60; echo "$hours:$minutes:$seconds"; ?>
这产生:
$ php file.php 0:11:25
(我没有testing过这么多,所以可能会出现地板错误)
干得好
function format_time($t,$f=':') // t = seconds, f = separator { return sprintf("%02d%s%02d%s%02d", floor($t/3600), $f, ($t/60)%60, $f, $t%60); } echo format_time(685); // 00:11:25
仅当秒数小于86400
(1天)时才使用函数gmdate()
) :
$seconds = 8525; echo gmdate('H:i:s', $seconds); # 02:22:05
参见: gmdate()
运行演示
将秒转换成“foot”格式无限制* :
$seconds = 8525; $H = floor($seconds / 3600); $i = ($seconds / 60) % 60; $s = $seconds % 60; echo sprintf("%02d:%02d:%02d", $H, $i, $s); # 02:22:05
请参阅: floor() , sprintf() , 算术运算符
运行演示
使用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:05
请参阅: DateTime :: __构造() , DateTime :: modify() , 克隆 , sprintf()
运行演示
结果的MySQL示例范围被约束为从-838:59:59
到838:59:59
:
SELECT SEC_TO_TIME(8525); # 02:22:05
请参阅: SEC_TO_TIME
运行演示
PostgreSQL例子:
SELECT TO_CHAR('8525 second'::interval, 'HH24:MI:SS'); # 02:22:05
运行演示
其他解决scheme使用gmdate
,但是在超过86400秒的边缘情况下会失败。 为了解决这个问题,我们可以简单地计算自己的秒数,然后让gmdate
将剩下的秒数计算为分钟/秒。
echo floor($seconds / 3600) . gmdate(":i:s", $seconds % 3600);
input: 6030
输出:1: 1:40:30
: 1:40:30
input: 2000006030
输出: 555557:13:50
// 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
gmdate("H:i:s", no_of_seconds);
如果no_of_seconds
大于1天(一天中的秒数),将不会在H:i:s
格式中给出时间。
它会忽略日价值,只给Hour:Min:Seconds
例如:
gmdate("H:i:s", 89922); // returns 0:58:42 not (1 Day 0:58:42) or 24:58:42
这是一个处理负秒和超过1天价值秒的单线程。
sprintf("%s:%'02s:%'02s\n", intval($seconds/60/60), abs(intval(($seconds%3600) / 60)), abs($seconds%60));
例如:
$seconds= -24*60*60 - 2*60*60 - 3*60 - 4; // minus 1 day 2 hours 3 minutes 4 seconds echo sprintf("%s:%'02s:%'02s\n", intval($seconds/60/60), abs(intval(($seconds%3600) / 60)), abs($seconds%60));
输出:-26:03:04
写这样的函数来返回一个数组
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; }
然后简单地调用这个函数:
secondsToTime(100);
输出是
Array ( [h] => 0 [m] => 1 [s] => 40 )
看到:
/** * 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; }
From: 将秒转换为天,小时,分和秒
gmtdate()函数对我来说不起作用,因为我正在追踪一个项目的工作时间,如果它超过了24小时,在减去24小时后会得到剩余的金额。 换句话说37小时变成13小时。 (所有如上所述的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
尝试这个:
date("H:i:s",-57600 + 685);
取自
http://bytes.com/topic/php/answers/3917-seconds-converted-hh-mm-ss
这个function我很有用,你可以扩展它:
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;}
为此使用DateTime的简单方法是:
$time = 60; //sec. $now = time(); $rep = new DateTime('@'.$now); $diff = new DateTime('@'.($now+$time)); $return = $diff->diff($rep)->format($format); //output: 01:04:65
这是一个简单的解决scheme,使您可以使用DateTime格式的方法。
解决scheme来自: https : //gist.github.com/SteveJobzniak/c91a8e2426bac5cb9b0cbc1bdbc45e4b
这是一个非常干净和简短的方法!
这段代码尽可能地避免了繁琐的函数调用和逐段的stringbuild立,以及人们为此所做的大而笨重的function。
它产生“1h05m00s”的格式,并使用分钟和秒的前导零,只要另一个非零时间组件在他们之前。
它会跳过所有空的主要组件,以避免给你无用的信息,如“0h00m01s”(而不是显示为“1s”)。
示例结果:“1s”,“1m00s”,“19m08s”,“1h00m00s”,“4h08m39s”。
$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'; }
如果要使代码更短(但可读性更低),则可以避免使用$converted
数组,而是将值直接放在sprintf()调用中,如下所示:
$duration = 1; // values 0 and higher are supported! $result = ltrim( sprintf( '%02dh%02dm%02ds', floor( $duration / 3600 ), floor( ( $duration / 60 ) % 60 ), ( $duration % 60 ) ), '0hm' ); if( $result == 's' ) { $result = '0s'; }
在上述两个代码段中,持续时间必须为0或更高。 负的持续时间不受支持。 但是,您可以使用以下替代代码来处理负面持续时间:
$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"
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); }
那么我需要的东西可以把秒数减less到几分钟和几秒,但是会超过24小时,而不是进一步减less到几天。
这是一个简单的function。 你可能可以改善它…但这里是:
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; }
以防其他人正在寻找一个简单的函数来返回这个很好的格式(我知道这不是OP所要求的格式),这就是我刚刚提出的。 感谢@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; }
任何人在未来寻找这个,这就给出了最初的海报要求的格式。
$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";
<?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, "; }