1

I need to compare two dates

d1= 12-11-01(YY-MM-DD) and d2=2008-05-02(YYYY-MM-DD)

comparison d2 <= d1

It is not working as per requirement

Here please note that I need to compare dates with different date formats.

Abhijit Shelar
  • 1,055
  • 7
  • 21
  • 41

6 Answers6

3

Try with:

$d1 = strtotime('12-11-01');
$d2 = strtotime('2008-05-02');

if ( $d2 <= $d1 ) {}
hsz
  • 148,279
  • 62
  • 259
  • 315
2

DateTime is better than strtotime() because it can account for timezones and daylight savings time

$dt1 = new DateTime('12-11-01');
$dt2 = new DateTime('2008-05-02');
if ( $dt2 <= $dt1 ) {

}

See it in action

John Conde
  • 217,595
  • 99
  • 455
  • 496
1

Do this :

if(strtotime('12-11-01') <= strtotime('2008-05-02'))
{
     // do your task here
}
Dead Man
  • 2,880
  • 23
  • 37
0

You can use this code:

$d1 = strtotime($date1);
$d2 = strtotime($date2);
if($d2 <= $d1){
    // $date2 is before $date1
}
TimWolla
  • 31,849
  • 8
  • 63
  • 96
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33
0

Try this

if(strtotime('12-11-01') <= strtotime('2008-05-02'))
{   }
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
0

Like below,

if(strtotime($date1) < strtotime($date2)) {
    // do something
}

Research it further here

http://php.net/manual/en/function.strtotime.php

GriffLab
  • 2,076
  • 3
  • 20
  • 21