This post was updated 757 days ago and some of the ideas may be out of date.
利用PHP的纳秒计时器获取当前时间精确到纳秒
<?php
/**
* Created by PhpStorm.
* User: LinFei
* Created time 2022/11/25 15:10:51
* E-mail: fly@eyabc.cn
*/
declare (strict_types=1);
date_default_timezone_set('Asia/Shanghai');
// 获取当前纳秒计时器时间
$currentHrtime = hrtime(true);
// 获取当前时间戳转换为纳秒
$time = microtime(true) * 1.0E9;
// 计算出纳秒计时器跟当前时间的时间差
$timeDiff = $time - $currentHrtime;
while (true) {
// 获取当前精确到纳秒的时间戳
$currentTime = hrtime(true) + $timeDiff;
var_dump(number_format($currentTime, 0, '', ''));
var_dump(date('Y-m-d H:i:s', intval($currentTime / 1.0E9)));
sleep(1);
}
参与讨论