我需要将秒转换为“时:分:秒”格式。
例如:“685” 转换为 “00:11:25”
我该如何实现?
编写如下函数以返回一个数组:
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 )
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;}
请参考:
/**
* 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;
}
试试这个:
date("H:i:s",-57600 + 685);
来源:
http://bytes.com/topic/php/answers/3917-seconds-converted-hh-mm-ss
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
不确定为什么还没有提出这个建议,但是这里有一个使用DateInterval
的变体,它具有秒数> 86400不是问题的优点(只需不期望小时<24即可)
请注意:该函数返回DateInterval
对象,因此输出格式由调用者决定。
function seconds2interval($seconds) {
$s = intval($seconds);
$hrs = floor($s / 3600);
$min = floor(($s - ($hrs * 3600)) / 60);
$sec = $s % 60;
$duration = sprintf('PT%dH%dM%dS', $hrs, $min, $sec);
$d = new DateInterval($duration);
return $d;
}
$d = seconds2interval(93837.3113);
echo $d->format('%H:%I:%s'); // 26:03:57
来源: https://gist.github.com/SteveJobzniak/c91a8e2426bac5cb9b0cbc1bdbc45e4b
这段代码尽可能减少冗长的函数调用和字符串构建,以及人们为此创建的大而笨重的函数。
它返回的输出格式为"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 readableSeconds($seconds)
{
$days = intdiv($seconds, 86400);
$hours = intdiv(($seconds % 86400), 3600);
$minutes = intdiv(($seconds % 3600), 60);
$seconds = $seconds % 60;
return ltrim(sprintf('%dd %dh %dm %ds', $days, $hours, $minutes, $seconds), ' 0dhm');
}
1天 1小时 8分钟 20秒
if (nobody_asked_you_nothing) continue;
@你的常识 如果(没人问你什么)继续;
- Victor Volpe使用 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
($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds)
将返回类似于以下的字符串:55.55 => '0:55'
100 => '1:40'
可能对于时间 >= 24 小时不起作用。