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

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

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


当前回答

使用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

这是一个简单的解决方案,让您能够使用DateTime的格式方法。

其他回答

以防其他人正在寻找一个简单的函数来返回格式化良好的格式(我知道这不是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";

其他解决方案使用gmdate,但在超过86400秒的边缘情况下会失败。为了解决这个问题,我们可以自己简单地计算小时数,然后让gmdate将剩余的秒计算为分/秒。

echo floor($seconds / 3600) . gmdate(":i:s", $seconds % 3600);

输入:6030 输出:1:40:30

输入:2000006030 输出:555557:13:50

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
<?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, ";
}