How to compare two dates in php if dates are in format '03_01_12'
and '31_12_11'
.
如果日期是“03_01_12”和“31_12_11”格式,如何在php中比较两个日期。
I am using this code:
我使用的代码是:
$date1=date('d_m_y');
$date2='31_12_11';
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
But its not working..
但它不工作。
13 个解决方案
#1
32
You will have to make sure that your dates are valid date objects.
您必须确保您的日期是有效的日期对象。
Try this:
试试这个:
$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));
You can then perform the strtotime()
method to get the difference.
然后,您可以执行strtotime()方法来获得差异。
#2
105
Your brackets are not all matched:
您的括号并非都匹配:
if(strtotime($date1))<strtotime($date2)))
Change to this:
改变:
if(strtotime($date1) < strtotime($date2))
#3
34
Using DateTime::createFromFormat:
使用DateTime::createFromFormat:
$format = "d_m_y";
$date1 = \DateTime::createFromFormat($format, "03_01_12");
$date2 = \DateTime::createFromFormat($format, "31_12_11");
var_dump($date1 > $date2);
#4
7
The date_diff() function returns the difference between two DateTime objects.
函数的作用是:返回两个DateTime对象之间的差值。
If the first date is before the second date a positive number of days will be returned; otherwise a negative number of days:
如果第一个日期是在第二个日期之前,则返回一个正数天数;否则天数为负数:
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
output will be "+272 days" ;
输出为“+272天”;
changing $date1 = "2014-03-15"
改变美元date1 = " 2014-03-15 "
<?php
$date1=date_create("2014-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
Output will be "-93 days"
输出将是“-93天”
#5
6
Extending @nevermind's answer, one can use DateTime::createFromFormat: like,
扩展@nevermind的答案,可以使用DateTime::createFromFormat: like,
// use - instead of _. replace _ by - if needed.
$format = "d-m-y";
$date1 = DateTime::createFromFormat($format, date('d-m-y'));
$date2 = DateTime::createFromFormat($format, str_replace("_", "-",$date2));
var_dump($date1 > $date2);
#6
5
<?php
$expiry_date = "2017-12-31 00:00:00"
$today = date('d-m-Y',time());
$exp = date('d-m-Y',strtotime($expiry_date));
$expDate = date_create($exp);
$todayDate = date_create($today);
$diff = date_diff($todayDate, $expDate);
if($diff->format("%R%a")>0){
echo "active";
}else{
echo "inactive";
}
echo "Remaining Days ".$diff->format("%R%a days");
?>
#7
4
you can try something like:
你可以试试:
$date1 = date_create('2014-1-23'); // format of yyyy-mm-dd
$date2 = date_create('2014-2-3'); // format of yyyy-mm-dd
$dateDiff = date_diff($date1, $date2);
var_dump($dateDiff);
You can then access the difference in days like this $dateDiff->d;
然后您可以访问不同的天,如$dateDiff->d;
#8
3
Don't know what you're problem is but:
不知道你的问题是什么,但是:
function date_compare($d1, $d2)
{
$d1 = explode('_', $d1);
$d2 = explode('_', $d2);
$d1 = array_reverse($d1);
$d2 = array_reverse($d2);
if (strtotime(implode('-', $d1)) > strtotime(implode('-', $d2)))
{
return $d2;
}
else
{
return $d1;
}
}
#9
3
Try this
试试这个
$data1 = strtotime(\date("d/m/Y"));
$data1 = date_create($data1);
$data2 = date_create("21/06/2017");
if($data1 < $data2){
return "The most current date is date1";
}
return "The most current date is date2";
#10
2
compare the result of maketime()
for each of the time
比较maketime()的结果
#11
2
I know this is late, but for future reference, put the date format into a recognised format by using str_replace then your function will work. (replace the underscore with a dash)
我知道这已经很晚了,但是为了以后的参考,请使用str_replace将日期格式转换为可识别的格式,这样您的函数就可以工作了。(用破折号替换下划线)
//change the format to dashes instead of underscores, then get the timestamp
$date1 = strtotime(str_replace("_", "-",$date1));
$date2 = strtotime(str_replace("_", "-",$date2));
//compare the dates
if($date1 < $date2){
//convert the date back to underscore format if needed when printing it out.
echo '1 is small='.$date1.','.date('d_m_y',$date1);
}else{
echo '2 is small='.$date2.','.date('d_m_y',$date2);
}
#12
2
You can to converte for integer number and compare.
可以对整数进行收敛并进行比较。
Eg.:
如:
$date_1 = date('Ymd'); $date_2 = '31_12_2011';
$ date_1 =日期(“Ymd”);美元date_2 = 31 _12_2011”;
$date_2 = (int) implode(array_reverse(explode("_", $date_2)));
美元date_2 =(int)内爆(array_reverse(爆炸(“_”,date_2美元)));
echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';
echo ($date_1 < $date_2) ?“$date_2大于$date_1”:“$date_2小于$date_1”;
#13
0
Not answering the OPs actual problem, but answering just the title. Since this is the top result for "comparing dates in php".
没有回答OPs的实际问题,只是回答了标题。因为这是“在php中比较日期”的最高结果。
Pretty simple to use Datetime Objects (v>= 5.3.0
) and Compare them directly
使用Datetime对象非常简单(v>= 5.3.0),并直接比较它们。
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
var_dump($date1 < $date2);
Note: Datetime object can also be created for a predefined date as below.
注意:Datetime对象也可以创建为预定义的日期,如下所示。
$date1 = new DateTime('2009-10-11');
#1
32
You will have to make sure that your dates are valid date objects.
您必须确保您的日期是有效的日期对象。
Try this:
试试这个:
$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));
You can then perform the strtotime()
method to get the difference.
然后,您可以执行strtotime()方法来获得差异。
#2
105
Your brackets are not all matched:
您的括号并非都匹配:
if(strtotime($date1))<strtotime($date2)))
Change to this:
改变:
if(strtotime($date1) < strtotime($date2))
#3
34
Using DateTime::createFromFormat:
使用DateTime::createFromFormat:
$format = "d_m_y";
$date1 = \DateTime::createFromFormat($format, "03_01_12");
$date2 = \DateTime::createFromFormat($format, "31_12_11");
var_dump($date1 > $date2);
#4
7
The date_diff() function returns the difference between two DateTime objects.
函数的作用是:返回两个DateTime对象之间的差值。
If the first date is before the second date a positive number of days will be returned; otherwise a negative number of days:
如果第一个日期是在第二个日期之前,则返回一个正数天数;否则天数为负数:
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
output will be "+272 days" ;
输出为“+272天”;
changing $date1 = "2014-03-15"
改变美元date1 = " 2014-03-15 "
<?php
$date1=date_create("2014-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
Output will be "-93 days"
输出将是“-93天”
#5
6
Extending @nevermind's answer, one can use DateTime::createFromFormat: like,
扩展@nevermind的答案,可以使用DateTime::createFromFormat: like,
// use - instead of _. replace _ by - if needed.
$format = "d-m-y";
$date1 = DateTime::createFromFormat($format, date('d-m-y'));
$date2 = DateTime::createFromFormat($format, str_replace("_", "-",$date2));
var_dump($date1 > $date2);
#6
5
<?php
$expiry_date = "2017-12-31 00:00:00"
$today = date('d-m-Y',time());
$exp = date('d-m-Y',strtotime($expiry_date));
$expDate = date_create($exp);
$todayDate = date_create($today);
$diff = date_diff($todayDate, $expDate);
if($diff->format("%R%a")>0){
echo "active";
}else{
echo "inactive";
}
echo "Remaining Days ".$diff->format("%R%a days");
?>
#7
4
you can try something like:
你可以试试:
$date1 = date_create('2014-1-23'); // format of yyyy-mm-dd
$date2 = date_create('2014-2-3'); // format of yyyy-mm-dd
$dateDiff = date_diff($date1, $date2);
var_dump($dateDiff);
You can then access the difference in days like this $dateDiff->d;
然后您可以访问不同的天,如$dateDiff->d;
#8
3
Don't know what you're problem is but:
不知道你的问题是什么,但是:
function date_compare($d1, $d2)
{
$d1 = explode('_', $d1);
$d2 = explode('_', $d2);
$d1 = array_reverse($d1);
$d2 = array_reverse($d2);
if (strtotime(implode('-', $d1)) > strtotime(implode('-', $d2)))
{
return $d2;
}
else
{
return $d1;
}
}
#9
3
Try this
试试这个
$data1 = strtotime(\date("d/m/Y"));
$data1 = date_create($data1);
$data2 = date_create("21/06/2017");
if($data1 < $data2){
return "The most current date is date1";
}
return "The most current date is date2";
#10
2
compare the result of maketime()
for each of the time
比较maketime()的结果
#11
2
I know this is late, but for future reference, put the date format into a recognised format by using str_replace then your function will work. (replace the underscore with a dash)
我知道这已经很晚了,但是为了以后的参考,请使用str_replace将日期格式转换为可识别的格式,这样您的函数就可以工作了。(用破折号替换下划线)
//change the format to dashes instead of underscores, then get the timestamp
$date1 = strtotime(str_replace("_", "-",$date1));
$date2 = strtotime(str_replace("_", "-",$date2));
//compare the dates
if($date1 < $date2){
//convert the date back to underscore format if needed when printing it out.
echo '1 is small='.$date1.','.date('d_m_y',$date1);
}else{
echo '2 is small='.$date2.','.date('d_m_y',$date2);
}
#12
2
You can to converte for integer number and compare.
可以对整数进行收敛并进行比较。
Eg.:
如:
$date_1 = date('Ymd'); $date_2 = '31_12_2011';
$ date_1 =日期(“Ymd”);美元date_2 = 31 _12_2011”;
$date_2 = (int) implode(array_reverse(explode("_", $date_2)));
美元date_2 =(int)内爆(array_reverse(爆炸(“_”,date_2美元)));
echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';
echo ($date_1 < $date_2) ?“$date_2大于$date_1”:“$date_2小于$date_1”;
#13
0
Not answering the OPs actual problem, but answering just the title. Since this is the top result for "comparing dates in php".
没有回答OPs的实际问题,只是回答了标题。因为这是“在php中比较日期”的最高结果。
Pretty simple to use Datetime Objects (v>= 5.3.0
) and Compare them directly
使用Datetime对象非常简单(v>= 5.3.0),并直接比较它们。
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
var_dump($date1 < $date2);
Note: Datetime object can also be created for a predefined date as below.
注意:Datetime对象也可以创建为预定义的日期,如下所示。
$date1 = new DateTime('2009-10-11');