检查给定日期是否已过去

时间:2010-07-21 09:09:45

标签: php date time

我有一个包含事件的周日历,并希望用户无法添加过去几天的事件。所以我想使用这样的函数:

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

它似乎工作正常,但它认为今天的日期是过去的一天。我做错了什么?

3 个答案:

答案 0 :(得分:11)

更简单 - &gt;

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

答案 1 :(得分:10)

时间戳永远不会只包含日期,但始终是当前秒。 strtotime($this->day)将在0:00返回今天的日期,而您现在正在比较11:12

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

答案 2 :(得分:0)

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