duanmu5039 2011-10-03 11:00
浏览 11
已采纳

为什么PHP不识别何时给出的日期是在今天的日期之后?

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".

  • 写回答

4条回答 默认 最新

  • dongwenhui8900 2011-10-03 11:07
    关注

    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"; }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。