PHP - 用微秒计算时间,整整一秒做出反应


PHP - Count the time with microseconds, react on a whole second

我正在使用php命令microTime(true)从服务器检索时间。

我想在每次达到一整秒时触发一个事件,我需要microsecomds的准确性来确定何时达到一整秒。

这是我到目前为止的代码:

$time_start = microtime(true);
while($someValue){
    if(microtime(true)==time()){
        echo "<p>".microtime(true)."</p>";
        echo "<p>".time()."</p>";
    }
    //sleep for one microsecond
    usleep(1);
} 

有人可以帮助或向我解释如何做到这一点吗?

为什么不只使用 usleep 一次?

usleep(1000);

你确定它会不那么精确吗?我不明白如何...

这段代码比你的更适合我

$time = time();
while (true) {
        $curtime = time();
        if ($curtime > $time) {
                $time = $curtime;
                echo microtime(true), "'r'n";
        }
        usleep(1);
}

您无法确定该处理器在每一微秒内都有时间处理您的过程。