Why I'm not geting proper date comparison result in following situation?

99 views Asked by At

My code is as follows:

if(!empty($form_data["birth_date"])){
          $date1   = date('d/m/y');

          $tempArr = explode('/', $form_data["birth_date"]);
          $date2   = date("d/m/y", mktime(0, 0, 0, $tempArr[1] ,$tempArr[0], $tempArr[2]));

        }

if(strtotime($date2) >= strtotime($date1)) 
          echo "Please enter date which is not greater than today's date!!!";

I'm getting the error message even after entering the date less than today's date. I'm using dd/mm/yyyy format. Can anyone help me please? Thanks in advance.

$date1 = 20/12/2013;

$date2 = 21/03/1985;
2

There are 2 answers

1
Manish Patel On

strtotime function is used to date format YYYY/MM/DD so please convert date this formate and then apply strtotime() and after compare two dates

2
Mahmood Rehman On

Use DateTime function for this . Try like this :

$date1 = new DateTime("now");
$date2 = new DateTime("12/12/2013 21:00:02");

var_dump($date1 == $date2);
var_dump($date1 < $date2);
var_dump($date1 > $date2);

OUTPUT:

bool(false)
bool(true)
bool(false)