Monday, 16 May 2016

How to compare two dates in php?

$d1=1/1/2011
$d2=1/2/2011
$result=-86400

If $comp is negative, you know that $d1 < $d2 
if $comp is positive, you know that $d1 > $d2 
if $comp == 0 or !$comp, you know that they are equal. 


In Certain Case we need to Compare Two Dates to find, weather today is greater then the other date or not.



function compareDates ($d1, $d2)
{
  return strtotime($d1)-strtotime($d2);
}

$d1 = "1/1/2008";
$d2 = "1/2/2008";

$comp = compareDates($d1, $d2);
If $comp is negative, you know that $d1 < $d2
if $comp is positive, you know that $d1 > $d2
if $comp == 0 or !$comp, you know that they are equal.




No comments:

Post a Comment