我需要将秒转换为“小时:分钟:秒”。
例如:“685”转换为“00:11:25”
我怎样才能做到这一点呢?
我需要将秒转换为“小时:分钟:秒”。
例如:“685”转换为“00:11:25”
我怎样才能做到这一点呢?
当前回答
仅当秒数小于86400(1天)时使用函数gmdate():
$seconds = 8525;
echo gmdate('H:i:s', $seconds);
# 02:22:05
参见: gmdate()
运行Demo
将秒转换为“脚”格式无限制*:
$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(),算术运算符
运行Demo
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::__construct(), DateTime::modify(), clone, sprintf ()
运行Demo
MySQL示例的结果范围被限制为TIME数据类型,从-838:59:59到838:59:59:
SELECT SEC_TO_TIME(8525);
# 02:22:05
看:SEC_TO_TIME
运行Demo
PostgreSQL的例子:
SELECT TO_CHAR('8525 second'::interval, 'HH24:MI:SS');
# 02:22:05
运行Demo
其他回答
仅当秒数小于86400(1天)时使用函数gmdate():
$seconds = 8525;
echo gmdate('H:i:s', $seconds);
# 02:22:05
参见: gmdate()
运行Demo
将秒转换为“脚”格式无限制*:
$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(),算术运算符
运行Demo
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::__construct(), DateTime::modify(), clone, sprintf ()
运行Demo
MySQL示例的结果范围被限制为TIME数据类型,从-838:59:59到838:59:59:
SELECT SEC_TO_TIME(8525);
# 02:22:05
看:SEC_TO_TIME
运行Demo
PostgreSQL的例子:
SELECT TO_CHAR('8525 second'::interval, 'HH24:MI:SS');
# 02:22:05
运行Demo
写这样的函数返回一个数组
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 )
不知道为什么还没有提出这个建议,但是这里有一个使用DateInterval的变体,它的优点是秒> 86400不是问题(只是不要期望小时< 24)
noteable:该函数返回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
这个函数是有用的,你可以扩展它:
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;}
如果你想创建一个音频/视频持续时间字符串,如YouTube等,你可以这样做:
($seconds >= 60) ?ltrim(函数(“我:年代”,美元秒),“0”):函数(“0:年代”,美元秒)
将返回如下字符串:
55.55 => '0:55'
100 => '1:40'
在>= 24小时内可能不会很好地工作。