我需要将秒转换为“小时:分钟:秒”。

例如:“685”转换为“00:11:25”

我怎样才能做到这一点呢?


当前回答

任何人在未来寻找这个,这给了最初的海报要求的格式。

$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";

其他回答

不知道为什么还没有提出这个建议,但是这里有一个使用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 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);
}

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

我已经在这里解释过了 把答案也粘贴到这里

在23:59:59小时之前,您可以使用PHP默认函数

echo gmdate("H:i:s", 86399);

只会返回结果直到23:59:59

如果你的秒数大于86399 在@VolkerK的帮助下回答

$time = round($seconds);
echo sprintf('%02d:%02d:%02d', ($time/3600),($time/60%60), $time%60);

将是使用…的最佳选择。