2012-05-07 54 views
0

我使用php命令microTime(true)从服务器检索时间。PHP - 以微秒为单位计算时间,在整秒内作出反应

我想在每次达到完整的秒数时触发一个事件,我需要用微秒的精度来确定何时达到完整的秒数。

这是我的代码至今:

$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); 
} 

有人能帮助或向我解释如何得到这个权利?

回答

2

为什么不只使用一次睡眠?

usleep(1000); 

你确定它会不太准确吗?我不知道怎样......

0

对于我这个代码更好地工作比你

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

你不能肯定的是,该处理器有时间你在每个微秒的过程。

相关问题