为什么 PHP 无法识别给定的日期是在今天之后的日期吗?

发布于 2024-12-08 07:02:12 字数 850 浏览 0 评论 0原文

我想在 PHP 中比较两个日期。 其中一个日期是到期日期,它由用户提供,存储在数据库中并使用 PHP 检索。 第二个是今天的日期。

    $unixdue = strtotime($query['date_due']); //Converts the database integer to Unix date
    $duestamp = date("dmY", $unixdue); //Converts the Unix date to dd-mm-yyyy


//If the due date is larger than today (i.e., after today), it's early.
        if ($query['complete'] == 0 AND date("dmY") < $duestamp) {echo "Early";}
//If the due date is smaller than today (i.e., before today), it's late.
        elseif ($query['complete'] == 0 AND date("dmY") > $duestamp) {echo "Late";}
//If the due date IS today, it's today.
        elseif ($query['complete'] == 0 AND date("dmY") == $duestamp) {echo "Today";}

它适用于今天的日期,但所有其他日期都“早”返回,即使它们不早。 因此,我什至无法判断其余部分是否正常工作。

我尝试比较两个 Unix 日期,但它们也包括时间,而实际上我只想要日期。如果我比较两个 UNIX 日期,我无法判断某件事是否是“今天”。

I want to compare two dates in PHP.
One date is the date due, which is provided by the user, stored in a database and retrieved with PHP.
The second is today's date.

    $unixdue = strtotime($query['date_due']); //Converts the database integer to Unix date
    $duestamp = date("dmY", $unixdue); //Converts the Unix date to dd-mm-yyyy


//If the due date is larger than today (i.e., after today), it's early.
        if ($query['complete'] == 0 AND date("dmY") < $duestamp) {echo "Early";}
//If the due date is smaller than today (i.e., before today), it's late.
        elseif ($query['complete'] == 0 AND date("dmY") > $duestamp) {echo "Late";}
//If the due date IS today, it's today.
        elseif ($query['complete'] == 0 AND date("dmY") == $duestamp) {echo "Today";}

It works with dates that are today, but all the others return "Early" even if they are not early.
Due to this I can't even tell if the rest is working at all.

I have tried comparing two Unix dates, but they include the time as well, when really I only want the date. If I compare two unix dates, I can't say if something is "Today".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沫尐诺 2024-12-15 07:02:12

您无法比较两个字符串是否小于或大于彼此。或者您可以,但在这种情况下您不应该,因为结果不是您想要的(例如“03092011”<“29082011”)。

您可以首先比较日期是否与您所做的相同;如果不是,您可以比较时间戳。即使您仅使用日期也没关系,因为无论时间部分如何,较早日期的时间戳始终小于较晚日期的时间戳。

if( $query['complete'] == 0 ) {
    if( date("dmY") == $duestamp ) { echo "Today"; }
    elseif( time() < $unixdue ) { echo "Early"; }
    elseif( time() > $unixdue ) { echo "Late"; }
}

You can't compare whether two strings are less than or greater than each other. Or you can, but in this case you shouldn't because the results are not what you want ("03092011" < "29082011" for example).

You can first compare if the dates are equal as you have done; if they are not, you can compare the timestamps. Even though you use only dates it doesn't matter because the timestamp of an earlier date is always smaller than the timestamp of a later date, regardless of the time part.

if( $query['complete'] == 0 ) {
    if( date("dmY") == $duestamp ) { echo "Today"; }
    elseif( time() < $unixdue ) { echo "Early"; }
    elseif( time() > $unixdue ) { echo "Late"; }
}
顾铮苏瑾 2024-12-15 07:02:12

您比较的方式是错误的。

一个可能的解决方案是使用 date('Ymd') 但更好的是使用时间戳作为 time()$unixdue

You are comparing in a wrong way.

A possible solution would be using date('Ymd') but even better is using timestamps as time() and the $unixdue

∞梦里开花 2024-12-15 07:02:12

使用 time 函数:

$msg = "Late";

if( $unixdue > time() ){
$msg = "Early";
}

if(date("dmY") === $duestamp){
$msg = "Today";
}

这是记录它的地方

Use the time function:

$msg = "Late";

if( $unixdue > time() ){
$msg = "Early";
}

if(date("dmY") === $duestamp){
$msg = "Today";
}

Here is where it is documented.

星軌x 2024-12-15 07:02:12

通过引入 unix 时间戳,你的代码变得过于复杂,这是完全没有必要的
并且看来您必须将完整性检查移至 SQL 查询中。所以

if     ($query['date_due'] > date("Y-m-d")) echo "Early";
elseif ($query['date_due'] < date("Y-m-d")) echo "Late";
else echo "Today";

我相信您也可以将此条件移至查询中

you have overcomplicated your code by introducing unix timestamp, absolutely unnecessarily
and and it seems you have to move completeness check into SQL query. So

if     ($query['date_due'] > date("Y-m-d")) echo "Early";
elseif ($query['date_due'] < date("Y-m-d")) echo "Late";
else echo "Today";

and I believe you can move this condition into query too

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文