简体   繁体   English

检查给定日期是否已过去

[英]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 . strtotime($this->day)将在今天的0:00返回,而你现在正在比较它,比如说, 11:12

You could use strtotime("$this->day 12:59:59pm"); 你可以使用strtotime("$this->day 12:59:59pm"); (if the format of $this->day allows for that) or use tomorrow 's timestamp. (如果$this->day的格式允许)或使用明天的时间戳。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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