PHP —? 将毫秒转换为小时 : 分钟 : Seconds.fractional

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4763668/
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 14:17:13  来源:igfitidea点击:

PHP —?Convert milliseconds to Hours : Minutes : Seconds.fractional

phpmillisecondsseconds

提问by AJB

I've got a script that takes in a value in seconds (to 2 decimal points of fractional seconds):

我有一个脚本,它以秒为单位接收一个值(到小数秒的 2 个小数点):

$seconds_input = 23.75

I then convert it to milliseconds:

然后我将其转换为毫秒:

$milliseconds = $seconds_input * 1000; // --> 23750

And then I want to format it like so:

然后我想像这样格式化它:

H:M:S.x // --> 0:0:23.75

Where 'x' is the fraction of the second (however many places after the decimal there are).

其中“x”是秒的分数(但小数点后有多少位)。

Any help? I can't seem to wrap my mind around this. I tried using gmdate() but it kept lopping off the fractional seconds.

有什么帮助吗?我似乎无法解决这个问题。我尝试使用 gmdate() 但它一直在削减小数秒。

Thanks.

谢谢。

回答by ircmaxell

Edit:Well, I was a bit hasty. Here's one way to do what you're asking:

编辑:嗯,我有点仓促。这是执行您所要求的一种方法:

function formatMilliseconds($milliseconds) {
    $seconds = floor($milliseconds / 1000);
    $minutes = floor($seconds / 60);
    $hours = floor($minutes / 60);
    $milliseconds = $milliseconds % 1000;
    $seconds = $seconds % 60;
    $minutes = $minutes % 60;

    $format = '%u:%02u:%02u.%03u';
    $time = sprintf($format, $hours, $minutes, $seconds, $milliseconds);
    return rtrim($time, '0');
}

回答by Peter Bailey

My take

我的看法

function formatSeconds( $seconds )
{
  $hours = 0;
  $milliseconds = str_replace( "0.", '', $seconds - floor( $seconds ) );

  if ( $seconds > 3600 )
  {
    $hours = floor( $seconds / 3600 );
  }
  $seconds = $seconds % 3600;


  return str_pad( $hours, 2, '0', STR_PAD_LEFT )
       . gmdate( ':i:s', $seconds )
       . ($milliseconds ? ".$milliseconds" : '')
  ;
}

And then the test

然后测试

$testData = array(
    23,              // Seconds, w/o millis
    23.75,           // Seconds, w/millis
    23.75123456789,  // Lots of millis
    123456789.75    // Lots of seconds
);

foreach ( $testData as $seconds )
{
  echo formatSeconds( $seconds ), PHP_EOL;
}

which yields

这产生

00:00:23
00:00:23.75
00:00:23.75123456789
34293:33:09.75

回答by GolezTrol

Mine is much less readable, so it must be better. :p

我的可读性要低得多,所以它必须更好。:p

Basically the same idea as @ircmaxell's version. It does trim the trailing '0's and even will skip the last '.' separator if milliseconds are 0.

与@ircmaxell 的版本基本相同。它确实修剪了尾随的 '0',甚至会跳过最后一个 '.'。如果毫秒为 0,则为分隔符。

<?

function format_period($seconds_input)
{
  $hours = (int)($minutes = (int)($seconds = (int)($milliseconds = (int)($seconds_input * 1000)) / 1000) / 60) / 60;
  return $hours.':'.($minutes%60).':'.($seconds%60).(($milliseconds===0)?'':'.'.rtrim($milliseconds%1000, '0'));
}

echo format_period(23.75);

回答by The Surrican

if you really want to do it using date function you are right, you have to deal with milliseconds externally, is only based on second-timestamps.

如果你真的想使用日期函数来做你是对的,你必须在外部处理毫秒,只基于第二个时间戳。

you could do something like this:

你可以做这样的事情:

<?
$input = "23.75";
$seconds = floor($input);
$date = DateTime::createFromFormat('s', floor($seconds));
$ms = ($input-$seconds);
if($ms == 0) {
$ms = "";
} else { 
$ms = ltrim($ms,"0,");
}
echo $date->format('H:i:s').$ms;

but be aware of the hour-overflow, if your hours exceed 24 you will probably end up discarding the days.

但请注意小时溢出,如果您的小时数超过 24,您可能最终会丢弃这些天。

in your case i would say your approach with floor to get the seconds is correct, and then you should probably just use modulo arithmetics like this:

在您的情况下,我会说您使用 floor 获取秒数的方法是正确的,然后您可能应该只使用像这样的模算术:

<?
$totalsecs = 86400*10;

$secs = $totalsecs%60;

echo "secs: $secs \n";

$minutes = ($totalsecs - $secs) % (60*60);

?> and so on..

?> 等等..