PHP日期时间大于今天 [英] PHP date time greater than today
本文介绍了PHP日期时间大于今天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?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';
}
PS:01/02/2016来自我的数据库
解决方案
您不是比较日期。您正在比较字符串。在'09/17/2015>'01/02/2016'的世界中,因为'09'>'01',您需要将日期放在可比较的字符串格式中,或者比较可比较的DateTime对象。
<?php
$ date_now = date(Ymd); //此格式是字符串可比
if($ date_now>'2016-01-02'){
echo'greater than';
} else {
echo'Less than';
}
或
<?php
$ date_now = new DateTime();
$ date2 = new DateTime(01/02/2016);
if($ date_now> $ date2){
echo'greater than';
} else {
echo'Less than';
}
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.
<?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
解决方案
You are not comparing 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.
<?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';
}
这篇关于PHP日期时间大于今天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文