如何在php中比较两个日期


108

如果日期格式为'03_01_12'和,如何在php中比较两个日期'31_12_11'

我正在使用此代码:

$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;

但是它不起作用..



Answers:


42

您必须确保您的日期是有效的日期对象。

试试这个:

$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]));

然后,您可以执行该strtotime()方法来获得差异。


2
为什么不使用str_replace?
self.name

36

使用DateTime :: createFromFormat

$format = "d_m_y";
$date1  = \DateTime::createFromFormat($format, "03_01_12");
$date2  = \DateTime::createFromFormat($format, "31_12_11");

var_dump($date1 > $date2);

1
使用d#m#Y时,任何受支持的分隔符均可;, :, (, ), /, ., ,, -使用。
2012年

9

date_diff()函数返回两个DateTime对象之间的差。

如果第一个日期早于第二个日期,则将返回正数天;否则为负数天:

<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>

输出将为“ +272天”;

更改$ 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");
    ?>

输出将为“ -93天”


8
<?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");
?>

你能补充些解释吗?
Prafulla Kumar Sahu 2015年

8

不是回答OP的实际问题,而是仅回答标题。由于这是“比较php中的日期”的最佳结果。

使用日期时间对象(php >= 5.3.0)并直接比较它们非常简单

$date1 = new DateTime("2009-10-11");
$date2 = new DateTime("tomorrow"); // Can use date/string just like strtotime.
var_dump($date1 < $date2);

DateTime类的对象无法转换为字符串
Hikaru Shindo


4

您可以尝试类似:

        $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);

然后,您可以像$ dateDiff-> d这样访问几天的差异;


嘿!谢谢,它的']'字符有错字吗?它靠近Enter键,我也必须一直按下它。感谢您的注意。
Frederick G. Sandalo 2014年

3

不知道您的问题是什么,但是:

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;
    }
}

3

试试这个

$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";


2

我知道这很晚,但是为了将来参考,请使用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);
}

2

您可以转换为整数并进行比较。

例如。:

$date_1 = date('Ymd');
$date_2 = '31_12_2011';

$date_2 = (int) implode(array_reverse(explode("_", $date_2)));

echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';

1

我觉得这个功能很简单

function terminateOrNotStringtoDate($currentDate, $terminationdate)
{
    $crtDate = new DateTime($currentDate);
    $termDate = new DateTime($terminationdate);
    if($crtDate >= $termDate)
    {
        return true;
    } else {
    return false;
    }
}

0

伙计们,请不要这么复杂简单的回答如下

$date1=date('d_m_y');
$date2='31_12_11';
$date1=str_replace('_', '-', $date1);
$date2=str_replace('_', '-', $date2)
if(strtotime($date1) < strtotime($date2))
   echo '1 is small ='.strtotime($date1).','.$date1;
else
   echo '2 is small ='.strtotime($date2).','.$date2;

我刚刚在您的代码中添加了两行


-1

如果两个日期的格式相同,则使用比较运算符。

$date1 = "2018-05-05"; 
$date2 = "2019-08-19"; 

//comparison operator to  

if ($date1 > $date2) {
    echo "$date1 is latest than $date2"; 
    }
else{
    echo "$date1 is older than $date2"; 
    }

输出:2018-05-05早于2019-08-19


仅当日期采用ISO 8601格式时,此方法才有效。
Dharman
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.