检查给定日期是否已过去

Luc*_*ano 10 php time date

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

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

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

小智 11

更简单 - >

if(strtotime($this->day) < strtotime(date('Y-m-d')))
{
   ...
}
else
{
   ...
}
Run Code Online (Sandbox Code Playgroud)


Pek*_*ica 10

时间戳永远不会仅包含日期,但始终低至当前秒.比如,现在strtotime($this->day)要回到今天的日期0:00,而你现在正在比较它11:12.

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