124

How to compare two dates in php if dates are in format '03_01_12' and '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..

Rana Aalamgeer
  • 702
  • 2
  • 8
  • 22
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106

16 Answers16

46

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.

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
dkulkarni
  • 2,780
  • 4
  • 27
  • 36
42

Using DateTime::createFromFormat:

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

var_dump($date1 > $date2);
nevvermind
  • 3,302
  • 1
  • 36
  • 45
  • 1
    When using d#m#Y, any of the supported seperators `;, :, (, ), /, ., ,, -` will work. – Rijk Jan 04 '12 at 08:21
27

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

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

$date1 = new DateTime("2009-10-11");
$date2 = new DateTime("tomorrow"); // Can use date/string just like strtotime.
var_dump($date1 < $date2);
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
12

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

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

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

Community
  • 1
  • 1
Rishabh
  • 546
  • 5
  • 8
10
<?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");
?>
Manikanta
  • 193
  • 3
  • 17
7

Extending @nevermind's answer, one can use 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);
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
5

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;

Adeel Gill
  • 353
  • 4
  • 19
  • hey! thanks, there was a little typo in it the ']' character right? its close to the Enter Key, I must have pressed it along too. Thanks for the look out. – Frederick G. Sandalo Dec 12 '14 at 10:20
3

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';
Dharman
  • 30,962
  • 25
  • 85
  • 135
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";
3

benchmark comparison date_create, strtotime, DateTime, and direct

direct is faster

$f1="2014-12-12";
$f2="2014-12-13";
$diff=false;


$this->bench('a');
for ($i=0; $i < 20000; $i++) { 
    $date1=date_create($f1);
    $date2=date_create($f2);
    $diff=date_diff($date1,$date2);
}
$this->bench('a','b');
for ($i=0; $i < 20000; $i++) { 
     $date1=strtotime($f1);
     $date2=strtotime($f2);
     if ($date1>$date2) {
        $diff=true;
    }
}
$this->bench('b','c');
for ($i=0; $i < 20000; $i++) { 
    $date1 = new DateTime($f1);
    $date2 = new DateTime($f2);
    if ($date1>$date2) {
        $diff=true;
     }
}
$diff=false;
$this->bench('c','d');
for ($i=0; $i < 20000; $i++) { 
     if ($f1>$f2) {
        $diff=true;
     }
}
$this->bench('d','e');
var_dump($diff);

$this->dumpr($this->benchs);

results:

    [a] => 1610415241.4687
    [b] => 1610415242.8759
    [a-b] => 1.407194852829
    [c] => 1610415243.5672
    [b-c] => 0.69137716293335
    [d] => 1610415244.7036
    [c-d] => 1.1363649368286
    [e] => 1610415244.7109
    [d-e] => 0.0073208808898926
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)

//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);
}
Alun
  • 21
  • 2
2

compare the result of maketime() for each of the time

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
ianace
  • 1,646
  • 2
  • 17
  • 31
2

Don't know what your 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;
    }
}
Xyz
  • 5,955
  • 5
  • 40
  • 58
devdRew
  • 4,393
  • 3
  • 24
  • 33
1

I think this one is very simple function

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

Guys Please don't make it so complex The simple answer bellow

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

I just have added two more lines with your code

Santi
  • 620
  • 8
  • 22
-1

If both dates are in the same format then use a comparison operator.

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

Output: 2018-05-05 is older than 2019-08-19

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140