2010-07-21 115 views
10

我有一个包含事件的星期日历,并且希望用户无法添加过去几天的事件。所以我特林使用函数那样:检查给定日期是否过去

if(strtotime($this->day) < time()){ // date format is YYYY-MM-DD 
// date is past 
}else{ 
// date is not past 
} 

它似乎工作得很好,但它认为今天的日期为过去的一天。我究竟做错了什么?

回答

10

时间戳从不包含日期,但始终下降到当前秒。​​将在今天的日期返回0:00,而您正在比较它与现在的比较,如11:12

您可以使用strtotime("$this->day 12:59:59pm");(如果格式为$this->day允许)或明天使用的时间戳。

+1

或者,使用'的strtotime($这个 - >天) user11977 2010-07-21 09:16:21

+0

谢谢你!这种方式很好! – Luciano 2010-07-21 09:18:04

11

简单 - >

if(strtotime($this->day) < strtotime(date('Y-m-d'))) 
{ 
    ... 
} 
else 
{ 
    ... 
} 
+0

谢谢你呢!我没有考虑今天在()函数中设置日期! – Luciano 2010-07-21 09:23:15

+0

strtotime函数...只是做了一个错字..对不起 – 2010-07-21 09:25:01

+0

如果你想也占了秒,添加H:我:s strtotime(日期('Y-m-d H:我:S'))' – justinl 2014-12-16 20:04:19

0
if(strtotime($this->day) < mktime(0, 0, 0)){ 
    // date is past 
} else { 
    // date is not past 
}