2013-03-21 91 views
1

我的目标是将当前时间与上次访问时间进行比较,如果是5分钟前,则允许再次访问,否则拒绝!这里是我到目前为止Strtotime按预期工作

$last_activate = strtotime($last_activ); //$last_activ is TIMESTAMP value retrieved from MySQL database 
$current_time = strtotime('now'); 

if(($current_time - $last_activate) > strtotime('5 minutes')){ 
    //allow access 
} 
else{ 
    //deny access 
} 

此刻,上面的代码总是执行else声明即使$last_activate为24小时前。任何人都知道我失踪的是什么?

+0

'$ last_activ'的价值是什么? – HamZa 2013-03-21 23:35:53

回答

5

strtotime('5 minutes')表示now + 5 minutes。证明:

echo date("Y-m-d H:i:s", strtotime('5 minutes')); 
2013-03-21 19:41:27 

而是执行此操作:

if(($current_time - $last_activate) > 5 * 60){ 
+0

谢谢!但是,请你解释一下为什么我以前的尝试总是'否认'?没有真正了解strtotime如何转换日期等。 – OutOfBoundsException 2013-03-21 23:46:51

+0

@PanayiotisNicolaou时差返回事件之间的秒数。例如120。虽然'strtotime(5分钟)'从现在开始5分钟返回时间戳,'1363917098'现在是值。你会发现它总是更大。 – Tchoupi 2013-03-22 00:48:06

2

更换5 minutes5 minutes ago

+2

只有在OP还删除'$ current_time'时才有效 – Steve 2013-03-21 23:38:57