我正在测量一些 curl 请求,我使用了 microtime(true)
。示例输出为 3.1745569706
这是 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
这看起来不对。我不太确定我在这里遗漏了什么。
如何将 microtime() 转换为 HH:MM:SS:UU ?
最佳答案
来自PHP.net article on 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().
改用这样的东西:
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
打印:00:00:03.1745569706
如果您愿意,可以使用 round()
对 $usec
var 进行更多舍入。
如果您使用 microtime(true)
,请改用它:
list($sec, $usec) = explode('.', microtime(true)); //split the microtime on .
关于php - 如何将 microtime() 转换为 HH :MM:SS:UU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16825240/