PHP 日期时间大于今天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32642417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:05:41  来源:igfitidea点击:

PHP date time greater than today

phpdate

提问by Nixxx27

please help whats wrong with my code. it always shows that today is greater than 01/02/2016 ? where in 2016 is greater than 2015.

请帮助我的代码有什么问题。它总是显示今天大于 01/02/2016 ?其中 2016 年大于 2015 年。

<?php
 $date_now = date("m/d/Y");

$date=date_create("01/02/2016");
$date_convert = date_format($date,"m/d/Y");

if ($date_now > $date_convert) {
        echo 'greater than';
    }else{
        echo 'Less than';
    }

P.S : 01/02/2016 is coming from my database

PS:01/02/2016 来自我的数据库

回答by John Conde

You are notcomparing dates. You are comparing strings. In the world of string comparisons, 09/17/2015> 01/02/2016because 09> 01. You need to either put your date in a comparable string format or compare DateTimeobjects which are comparable.

不是在比较日期。您正在比较字符串。在字符串比较的世界中,09/17/2015>01/02/2016因为09> 01。您需要将日期放在可比较的字符串格式中,或者比较DateTime可比较的对象。

<?php
 $date_now = date("Y-m-d"); // this format is string comparable

if ($date_now > '2016-01-02') {
    echo 'greater than';
}else{
    echo 'Less than';
}

Demo

演示

Or

或者

<?php
 $date_now = new DateTime();
 $date2    = new DateTime("01/02/2016");

if ($date_now > $date2) {
    echo 'greater than';
}else{
    echo 'Less than';
}

Demo

演示