php 如何将 microtime() 转换为 HH:MM:SS:UU

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

How to convert microtime() to HH:MM:SS:UU

phpmicrotime

提问by ProDraz

I was measuring some curl requests and I used microtime(true). The example output would be 3.1745569706

我正在测量一些 curl 请求,我使用了microtime(true). 示例输出将是3.1745569706

This is 3.1745569706seconds. I want to convert that to a somewhat more readable format, let's say 00:00:03:17455(HOURS:MINUTES:SECONDS:MILLISECONDS)

这是3.1745569706秒。我想将其转换为更易读的格式,比如说00:00:03:17455(HOURS:MINUTES:SECONDS:MILLISECONDS)

$maxWaitTime = '3.1745569706';
echo gmdate("H:i:s.u", $maxWaitTime);

// which returns
00:00:01.000000

echo date("H:i:s.u" , $maxWaitTime)
// which returns
18:00:01.000000

That looks wrong. I'm not quite sure what I'm missing here.

那看起来不对。我不太确定我在这里错过了什么。

How do I convert microtime() to HH:MM:SS:UU ?

如何将 microtime() 转换为 HH:MM:SS:UU ?

回答by edwardmp

From the PHP.net article on date()which is similar to gmdate(), except that the time is returned in GMT:

来自与类似的PHP.net 文章date()gmdate(),不同之处在于时间以 GMT 返回:

Since this function only accepts integer timestamps the u format character is only useful when using the date_format() function with user based timestamps created with date_create().

由于此函数仅接受整数时间戳,因此 u 格式字符仅在将 date_format() 函数与使用 date_create() 创建的基于用户的时间戳一起使用时有用。

Use something like this instead:

使用类似这样的东西:

list($usec, $sec) = explode(' ', microtime()); //split the microtime on space
                                               //with two tokens $usec and $sec

$usec = str_replace("0.", ".", $usec);     //remove the leading '0.' from usec

print date('H:i:s', $sec) . $usec;       //appends the decimal portion of seconds

Which prints: 00:00:03.1745569706

哪个打印: 00:00:03.1745569706

If you want you can use round()to round the $usecvar even more.

如果你愿意,你可以使用round()更圆的$usecvar。

If you use microtime(true)use this instead:

如果您microtime(true)使用它来代替:

list($sec, $usec) = explode('.', microtime(true)); //split the microtime on .

回答by Yogus

<?php

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(3.1745569706);

OUTPUT

输出

0:0:3.174

回答by Jeff

Assuming one really cares about microseconds, which is admittedly rare, then one should not use any representation that involves floats.

假设人们真的关心微秒,这无疑是罕见的,那么不应该使用任何涉及浮点数的表示。

Instead use gettimeofday() which will return an associative array that contains the seconds and microseconds as integers.

而是使用 gettimeofday() ,它将返回一个关联数组,其中包含作为整数的秒和微秒。

$g1 = gettimeofday();
# execute your process here
$g2 = gettimeofday();

$borrow  = $g2['usec'] < $g1['usec'] ;
$seconds = $g2['sec'] - $g1['sec'] - $borrow ;
$micros  = $borrow*1000000 + $g2['usec'] - $g1['usec'] ;
$delta   = gmdate( 'H:i:s.', $seconds ).sprintf( '%06d', $micros );