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

标签 php microtime

我正在测量一些 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/

相关文章:

PHP代码没有被执行,但是代码显示在浏览器源代码中

php - AWS RDS 的外键约束不起作用

php - 使用 MySQL 条目将按钮连接到 JavaScript 函数

php 发送 404 header 问题

php - 用微时间测量持续时间随机结果为零

Node.js:process.hrtime 什么时候开始

php - 如何从多维数组中插入排序数据

javascript - 将 microtime(true) 转换为 javascript 日期对象

php - gettimeofday() 总是可以访问?

php - 如何从php中的任何日期获取以微秒为单位的任何日期时间