Rohan Kumar Rohan Kumar - 1 year ago 74
PHP Question

How to compare two dates in php

How to compare two dates in php if dates are in format


I am using this code:

if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
echo '2 is small ='.strtotime($date2).','.$date2;

But its not working..

Answer Source

You will have to make sure that your dates are valid date objects.

Try this:

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download