我想做点什么:
$time = time();
//Store the time in the dabase
//Some time later, say three hours this code runs
// so if your time() was 2pm its now 5pm when this statement
// is run.
if($time < 4 hours){
// do something.
}
但我不确定最干净的方法是什么。
答案 0 :(得分:2)
OOP式
$start = new DateTime;
// Do something
if ($start < new DateTime('-4 hours')) {
// Do something different
}
非OOP方式也很简单
$start = time();
// Do something
if ($start < strtotime('-4 hours')) {
// Do something different
}
答案 1 :(得分:1)
time()会在几秒钟内回复php时间。当你的第二个代码块运行时,你想再次检查时间(),所以你应该这样做:
$timeNow = time();
if($savedTime < $timeNow-14400){
// do something
}
其中14400是4小时(60 * 60 * 4 == 14400)秒。当然,可能没有理由将time()设置为变量,但以防万一。