输出结果为秒。在php中将其转换为hh:mm:ss格式。

62
  1. 我的输出格式为290.52262423327秒,如何将其更改为00:04:51?

  2. 我想用秒和HH:MM:SS格式显示相同的输出,因此如果是秒,我只想显示290.52秒(小数点后只有两个整数)。 我该怎么做?

我正在使用php工作,输出存在$time变量中。希望将$time更改为具有HH:MM:SS格式的$newtime和具有290.52的$newsec

谢谢 :)


你是在寻找一个持续时间还是一个时间戳? - Rob K
可能是将秒转换为小时:分钟:秒的重复问题。 - PeerBr
你目前尝试了什么?你卡在哪里了?如果你能展示所使用的代码,那么可以帮助找出问题。 - Nico Haase
16个回答

134
1)
function foo($seconds) {
  $t = round($seconds);
  return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
}

echo foo('290.52262423327'), "\n";
echo foo('9290.52262423327'), "\n";
echo foo(86400+120+6), "\n";

打印
00:04:51
02:34:51
24:02:06

2)

echo round($time, 2);

我遇到了一些极端的不成功的除法和减法函数,试图在一行代码中实现你所做的事情 :) sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60) - adrianTNT

26

试试这个

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

此解决方案适用于小于一天秒数的任何值,但对于大于等于86400的值将重置为00:00:00。 - dctucker
2
有没有任何想法可以使其适用于大于86400的值? - Vinod Kumar
这是误导性的:gmdate()函数接受一个Unix时间戳作为第二个参数,因此实际上您将在1970年1月1日00:00:00 GMT之后90秒获得“H:i:s”部分,当然,如果您超过一天的秒数(86400),您将获得下一天的“H:i:s”部分。只有在保证总秒数不到一天时才使用它。 - Duncanmoo

4

编辑: 有评论指出,之前的答案在秒数超过一天(86400秒)时会失败。这是更新后的版本。原帖没有说明这个要求,所以这可能与原帖期望的实现方式不同,并且这里可能已经有更好的答案了。我只是不能忍受提供一个带有这个错误的答案。

$iSecondsIn = 290.52262423327;

// Account for days.
$iDaysOut = 0;
while ($iSecondsIn >= 86400) {
    $iDaysOut += 1;
    $iSecondsIn -= 86400;
}

// Display number of days if appropriate.
if ($iDaysOut > 0) {
    print $iDaysOut.' days and ';
}

// Print the final product.
print date('H:i:s', mktime(0, 0, $iSecondsIn));

旧版本,存在缺陷:

$iSeconds = 290.52262423327;
print date('H:i:s', mktime(0, 0, $iSeconds));

如果您的 $iSeconds 超过了 86399(一天中的秒数),那么这个答案也会有问题。86401 将会给您返回 00:00:01。 - Duncanmoo
谢谢您指出这一点。我已经相应地更新了答案。 - Teekin

4

截止到晚上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);

将是使用的最佳选项...


3

基于https://dev59.com/9XA65IYBdhLWcg3w-jym#3534705,但增加了天数:

function durationToString($seconds) {
  $time = round($seconds);

  return sprintf(
    '%02dD:%02dH:%02dM:%02dS',
    $time / 86400,
    ($time / 3600) % 24,
    ($time / 60) % 60,
    $time % 60
  );
}

2

试试这个:

$time = 290.52262423327;
echo date("h:i:s", mktime(0,0, round($time) % (24*3600)));

1

就我个人而言,根据其他人的答案,我制作了自己的解析器。可以处理天、小时、分钟和秒,并且应该易于扩展到周/月等。它还可以与C#反序列化一起使用。

function secondsToTimeInterval($seconds) {
    $t = round($seconds);
    $days = floor($t/86400);
    $day_sec = $days*86400;
    $hours = floor( ($t-$day_sec) / (60 * 60) );
    $hour_sec = $hours*3600;
    $minutes = floor((($t-$day_sec)-$hour_sec)/60);
    $min_sec = $minutes*60;
    $sec = (($t-$day_sec)-$hour_sec)-$min_sec;
    return sprintf('%02d:%02d:%02d:%02d', $days, $hours, $minutes, $sec);
}

1

逐步添加部分的简单格式化程序 - 示例:

  • formatTime(123) => 2m 3s
  • formatTime(7400) => 2h 3m 20s
  • formatTime(999999) => 11d 13h 46m 39s

function formatTime($secs)
{
    $secs = max(0, intval($secs));
    if($secs > 0){
        $out = [];
        $yrs = floor($secs / 31536e3);
        if($yrs){
            $out[] = $yrs."y";
        }
        $rem = $secs - $yrs * 31536e3;
        $days = floor($rem / 86400);
        if($days || $out){
            $out[] = $days."d";
        }
        $rem -= $days * 86400;
        $hrs = floor($rem / 3600);
        if($hrs || $out){
            $out[] = $hrs."h";
        }
        $rem -= $hrs * 3600;
        $min = floor($rem / 60);
        if($min || $out){
            $out[] = $min."m";
        }
        $rem -= $min * 60;
        $out[] = $rem."s";
        return implode(" ", $out);
    }
    return 0;
}

1

试试这个 :)

private function conversionTempsEnHms($tempsEnSecondes)
    {
        $h = floor($tempsEnSecondes / 3600);
        $reste_secondes = $tempsEnSecondes - $h * 3600;

        $m = floor($reste_secondes / 60);
        $reste_secondes = $reste_secondes - $m * 60;

        $s = round($reste_secondes, 3); 
        $s = number_format($s, 3, '.', '');

        $h = str_pad($h, 2, '0', STR_PAD_LEFT);
        $m = str_pad($m, 2, '0', STR_PAD_LEFT);
        $s = str_pad($s, 6, '0', STR_PAD_LEFT);

        $temps = $h . ":" . $m . ":" . $s;

        return $temps;
    }

1

我不知道这是否是最有效的方法,但如果您还需要显示天数,这将起作用:

function foo($seconds) { 
$t = round($seconds); 
return sprintf('%02d  %02d:%02d:%02d', ($t/86400%24), ($t/3600) -(($t/86400%24)*24),($t/60%60), $t%60);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接