简体   繁体   English

PHP-检查日期是过去的日期,但不包括今天的日期

[英]PHP - Check date is in the past but exclude todays date

I'm using the following function to check whether the date is in the past, but I want to exclude today's date from it as it returns true for today's date also. 我正在使用以下函数检查日期是否为过去日期,但我想从中排除今天的日期,因为它也对今天的日期返回true。

For example, if I pass today's date 2018-03-15, it returns true when it shouldn't. 例如,如果我经过今天的日期2018年3月15日,则当它不应该返回true时。

function is_date_in_past($date):bool
{
   return !empty($date) && (strtotime($date) < time());
}

Can anybody please help me with this, that how to exclude today's date from being recognised as past date? 有人可以帮我吗,如何将今天的日期排除在过去的日期之外?

Thanks in advance. 提前致谢。

Use DateTime objects. 使用DateTime对象。 They are easier to use than the old date & time functions and the code is more clear. 它们比旧的日期和时间函数更易于使用,并且代码更清晰。

function is_date_in_past($date) {
    return new DateTime($date) < new DateTime("today");
}

If you'd like to use old date/time functions, you can do like that: 如果您想使用旧的日期/时间函数,可以这样做:

 function is_date_in_past(string $date) :bool
 {
     return !empty($date) && strtotime($date) < strtotime(date('Y-m-d'));
 }

but better to consider to use https://stackoverflow.com/a/49305230/9254020 variant 但最好考虑使用https://stackoverflow.com/a/49305230/9254020变体

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

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