php 如何在php中比较两个日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8722806/
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-26 05:21:26  来源:igfitidea点击:

How to compare two dates in php

php

提问by Rohan Kumar

How to compare two dates in php if dates are in format '03_01_12'and '31_12_11'.

如何比较PHP的两个日期,如果日期格式'03_01_12''31_12_11'

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..

但它不起作用..

采纳答案by dkulkarni

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()方法来获得差异。

回答by nevvermind

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

回答by Rishabh

The date_diff() function returns the difference between two DateTime objects.

date_diff() 函数返回两个 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 天”

回答by Manikanta

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

回答by Mohd Abdul Mujib

Not answering the OPs actual problem, but answering just the title. Since this is the top result for "comparing dates in php".

不回答 OP 的实际问题,而只回答标题。因为这是“在 php 中比较日期”的最佳结果。

Pretty simple to use Datetime Objects (php >= 5.3.0) and Compare them directly

使用日期时间对象 ( 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);

回答by Rohan Kumar

Extending @nevermind's answer, one can use DateTime::createFromFormat:like,

扩展@nevermind的答案,可以使用DateTime::createFromFormat:像,

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

回答by Frederick G. Sandalo

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;

回答by devdRew

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

回答by Epitácio Bessa

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

回答by Danilo Pereira

You can to converte for integer number and compare.

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

Eg.:

例如。:

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