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
PHP date time greater than today
提问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/2016
because 09
> 01
. You need to either put your date in a comparable string format or compare DateTime
objects 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';
}
Or
或者
<?php
$date_now = new DateTime();
$date2 = new DateTime("01/02/2016");
if ($date_now > $date2) {
echo 'greater than';
}else{
echo 'Less than';
}