简体   繁体   中英

Check if a given date is past

I have a week calendar that holds events, and want that users can't add events for the past days. So I'm tring to use a function like that:

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

It seems to works fine, except that it consider today date as a past day. What am i doing wrong?

Simpler ->

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

A timestamp never contains only the date, but is always down to the current second. strtotime($this->day) is going to return today's date at 0:00 , while you are comparing it against now, say, 11:12 .

You could use strtotime("$this->day 12:59:59pm"); (if the format of $this->day allows for that) or use tomorrow 's timestamp.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM