Time()是以秒为单位的——有以毫秒为单位的吗?
当前回答
试试这个:
public function getTimeToMicroseconds() {
$t = microtime(true);
$micro = sprintf("%06d", ($t - floor($t)) * 1000000);
$d = new DateTime(date('Y-m-d H:i:s.' . $micro, $t));
return $d->format("Y-m-d H:i:s.u");
}
其他回答
我个人使用这个:
public static function formatMicrotimestamp(DateTimeInterface $dateTime): int
{
return (int) substr($dateTime->format('Uu'), 0, 13);
}
简单的回答是:
$milliseconds = floor(microtime(true) * 1000);
如果你想看到真正的微秒,你需要将php.ini中的精度设置更改为16。
在此之后,microsecond(true)为我提供了1631882476.298437的输出。
所以我认为我需要用余数(298437)除以1000,但实际上,余数是0.298437秒。所以我需要把它乘以1000才能得到正确的结果。
function get_milliseconds()
{
$timestamp = microtime(true);
return (int)(($timestamp - (int)$timestamp) * 1000);
}
echo date('Y-m-d H:i:s.')。gettimeofday()(“购买”);
输出:
2016-11-19 15:12:34.346351
$the_date_time = new DateTime($date_string);
$the_date_time_in_ms = ($the_date_time->format('U') * 1000) +
($the_date_time->format('u') / 1000);
推荐文章
- 如何从一个查询插入多行使用雄辩/流利
- 如何解析unix时间戳到时间。时间
- 如何使用yyyyMMddHHmmss格式格式化当前时间?
- SQL Developer只返回日期,而不是时间。我怎么解决这个问题?
- 在PHP单元测试执行期间,如何在CLI中输出?
- 在PHP中使用heredoc的优势是什么?
- PHP中的echo, print和print_r有什么区别?
- 前一个月的Python日期
- 如何将XML转换成PHP数组?
- 如何将对象转换为数组?
- 从IP地址获取位置
- 获取数组值的键名
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK
- PHP -获取bool值,当为false时返回false
- 在foreach中通过引用传递