简体   繁体   English

如果语句使用日期和 strtotime 返回错误结果

[英]If statement returning false result using date and strtotime

I'm trying to use an if statement to say "If today is between two dates, do this..." etc.我正在尝试使用 if 语句来表示“如果今天介于两个日期之间,请执行此操作……”等。

It's not working, and I'm at a total loss because as far as I can see - it SHOULD work!它不起作用,我完全不知所措,因为据我所知 - 它应该起作用!

if (date('d/m/y', strtotime('now')) >= date('d/m/y', strtotime('1 January 2021')) && date('d/m/y', strtotime('now')) <= date('d/m/y', strtotime('31 January 2021'))):
    echo 'true';
else:
    echo 'false';
endif;

Today is 3rd of December 2020 - so why is it telling me this statement is true?今天是 2020 年 12 月 3 日 - 为什么它告诉我这个说法是真的?

PS. PS。 It does work if I change it to either, or both > 30th December 2020 and < 1st February 2021 - but I'm wary to do this in case it's just a glitch and I've made an obvious coding mistake.如果我将其更改为> 30th December 2020< 1st February 2021两者之一,它确实有效 - 但我很谨慎这样做,以防它只是一个小故障并且我犯了一个明显的编码错误。

First of all.首先。 It's not December 3rd now it's the first of December.现在不是 12 月 3 日,而是 12 月 1 日。

The problem you have is because you are trying to compare strings.您遇到的问题是因为您正在尝试比较字符串。
Date() returns strings and as far as I know no programming language can properly (whatever properly means in this context) compare > or < with strings. Date() 返回字符串,据我所知,没有任何编程语言可以正确地(在这种情况下正确地表示)将 > 或 < 与字符串进行比较。 It's either === or.==.要么 === 要么.==。

So use the Unix times in the comparison since that is numeric and much easier for the computer to understand.因此,在比较中使用 Unix 次,因为这是数字,计算机更容易理解。

$now = strtotime('now');
$start = strtotime('1 January 2021');
$end = strtotime('31 January 2021');

if ($now >= $start && $now <= $end):
    echo 'true';
else:
    echo 'false';
endif;

https://3v4l.org/KAvQ2 https://3v4l.org/KAvQ2

date('d/m/y') gives you a string like 30/11/20 . date('d/m/y')给你一个像30/11/20这样的字符串。 Comparing dates in this format really means you're comparing strings, which only works if the dates are in YYYYMMDD (or YYYY-MM-DD) format.以这种格式比较日期实际上意味着您正在比较字符串,这仅在日期为 YYYYMMDD(或 YYYY-MM-DD)格式时才有效。

It's much easier to just use strtotime() to turn the English-like date into a Unix timestamp (just a big integer) and compare them directly:使用strtotime()将类似英语的日期转换为 Unix 时间戳(只是一个大整数)并直接比较它们要容易得多:

if ( 
    strtotime('now') >= strtotime('1 January 2021')
        &&
    strtotime('now') <= strtotime('31 January 2021')
) {
    echo 'true';
} else {
    echo 'false';
}

You really should be using DateTime class for all your date-related calculations.您确实应该使用DateTime class 进行所有与日期相关的计算。 Don't use the old strtotime() or date() unless you really know what you're doing.除非您真的知道自己在做什么,否则不要使用旧的strtotime()date()

With DateTime class it's really simple:使用DateTime class 非常简单:

<?php

$start = new DateTime('1 January 2021');
$end = new DateTime('31 January 2021');
$today = new DateTime();
if($today >= $start && $today <= $end){
    echo 'In between';
} else {
    echo 'outside or range';
}

Demo演示

Replace your code with following.用以下代码替换您的代码。

if (strtotime(date('d/m/y', strtotime('now'))) >= strtotime(date('d/m/y', strtotime('1 January 2021'))) && strtotime(date('d/m/y', strtotime('now'))) <= strtotime(date('d/m/y', strtotime('31 January 2021')))):
    echo 'true';
else:
    echo 'false';
endif;

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

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