PHP: wait 2 minute until next, is this right?
$timenow = tim开发者_StackOverflow中文版e();
if($lasttime - 120 > $timenow)
Is this right? to check if there has been 2 minutes ( 120 seconds ) since lasttime?
It's either $lasttime + 120 > $timenow
or $timenow - 120 > $lasttime
.
Imagine both times start with 0 and the "timenow" grows with every second. With that mindtrick you should get it.
if (($lasttime+120)>=time())
I suggest you to calculate number of elapsed seconds first, then compare it to your needs:
$elapsed_time = time() - $lasttime; ## time() is increasing
if ($elapsed_time > 120) {
## more then 2 minutes passed
}
else {
## it's not time yet
}
This should help to understand your code later.
Use
$time_start = microtime(true);
//Do whatever
if ($time_start-microtime(true)>=2000){
//Yup, 2 minutes
}
精彩评论