how php compare two dateTime object with comparator operator
$date1=new DateTime("07-03-2010");
$date2=new Datetime("21-09-2015");
if($date2>$date1)
{
echo "two datetime object has compared";
}
how php compare two DateTime object
Moderators: egami, macek, gesf
The DateTime class has a method called diff(). Use that to compare dates
example:
if the $interval is a positive value than $date1 is greater than $date2.
example:
Code: Select all
$interval = $date1->diff($date2);
echo $interval->format('%R%a days');
when i use comparator operator then also it give correct result
i.e Below code give correct result
if($date2>$date1)
{
echo "date2 is greater than date1";
}
BUT WHY?
i.e Below code give correct result
if($date2>$date1)
{
echo "date2 is greater than date1";
}
BUT WHY?
If your version of php allowes to compare the class instances ($date1 and $date2) then please go ahead and follow that route.
http://www.php.net/manual/en/datetime.construct.php
http://www.php.net/manual/en/datetime.construct.php