php 输出以秒为单位。在php中转换为hh:mm:ss格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3534533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:07:10  来源:igfitidea点击:

Output is in seconds. convert to hh:mm:ss format in php

phptime

提问by Scorpion King

  1. My output is in the format of 290.52262423327 seconds. How can i change this to 00:04:51?

  2. The same output i want to show in seconds and in HH:MM:SS format, so if it is seconds, i want to show only 290.52 seconds.(only two integers after decimal point)? how can i do this?

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

  2. 我想以秒和 HH:MM:SS 格式显示相同的输出,所以如果是秒,我只想显示 290.52 秒。(小数点后只有两个整数)?我怎样才能做到这一点?

I am working in php and the output is present in $timevariable. want to change this $timeinto $newtimewith HH:MM:SS and $newsecas 290.52.

我在 php 中工作,输出存在于$time变量中。想把它$time改成$newtimeHH:MM:SS 和$newsec290.52。

Thanks :)

谢谢 :)

回答by VolkerK

1)

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

prints

印刷

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

2)

2)

echo round($time, 2);

回答by Azam Alvi

Try this one

试试这个

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

回答by Teekin

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

回答by Martin Vseticka

Try this:

尝试这个:

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

回答by Serge

I dont know if this is the most efficient way, but if you also need to display days, this works:

我不知道这是否是最有效的方法,但是如果您还需要显示天数,则可以这样做:

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);
}

回答by Zagloo

Try this :)

尝试这个 :)

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;
    }

回答by Mannu saraswat

For till 23:59:59hours you can use PHP default function

直到23:59:59几个小时,您都可以使用 PHP 默认函数

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

Which will only return the result till 23:59:59

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

If your seconds is more then 86399 than with the help of @VolkerK answer

如果您的秒数超过 86399,而不是在@VolkerK 的帮助下回答

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

will be the best options to use ...

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

回答by GuyPaddock

Based on https://stackoverflow.com/a/3534705/4342230, but adding days:

基于https://stackoverflow.com/a/3534705/4342230,但添加天数:

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

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

回答by Rob K

1)

1)

$newtime = sprintf( "%02d:%02d:%02d", $time / 3600, $time / 60 % 60, $time % 60 );

2)

2)

$newsec = sprintf( "%.2f", $time );

回答by PassiveModding

Personally, going off other peoples answers I made my own parser. Works with days, hours, minutes and seconds. And shouldbe easy to expand to weeks/months etc. It works with deserialisation to c# as well

就个人而言,脱离其他人的答案,我制作了自己的解析器。适用于天、小时、分钟和秒。并且应该很容易扩展到周/月等。它也适用于反序列化到 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);
}