Page 1 of 1

PHP Date Comparison Code

Posted: Mon Jul 26, 2010 4:07 am
by Shasha Munzara
Unfortunately, PHP does not have an inbuilt date comparison function, like Visual Basic's DateDiff() function, to calculate the difference between 2 dates. But the code below can be used to calculate date differences.

Code: Select all

<?
 $Early_Date = $_REQUEST['Early_Date'];
 $Later_Date = $_REQUEST['Later_Date'];

 //details of the earlier date
 $dteComp = date('d',strtotime($Early_Date));
 $mnthComp = date('m',strtotime($Early_Date));
 $mnth = date('F',strtotime($Early_Date));
 $yr = date('Y',strtotime($Early_Date));
 
 //details of the later date
 $Later_Mnth = date('F',strtotime($Later_Date));
 $Later_MnthComp = date('m',strtotime($Later_Date));
 $Later_Yr = date('Y',strtotime($Later_Date));
 $Later_Day = date('d',strtotime($Later_Date));
 
 //The difference in years can easily be calculated by subtracting the earlier from the later month
 $Yr_Diff = $Later_Yr-$yr;
 
 //variable to check whether the current year is a leap year
 $chkLyr = $Later_Yr;
 
 //if the dates are of the same year
 if($Later_Yr==$yr)
 {
  $mnthDiff = $Later_MnthComp - $mnthComp;
  $difference = $mnthDiff;
 }
 else if($Later_Yr>$yr)
 {
  $chkctr=0;
  $difference=0;
  
 //Get the difference in months
  while($Later_Yr>$yr)
  {
	  if($chkctr==0)
	  {
	   $mnthDiff = $Later_MnthComp - 0;
	   $chkctr = 1;
	  }
	  else
	  {
	   $mnthDiff = 12;
	  }
	  
	  $difference+=$mnthDiff;
	  $Later_Yr=$Later_Yr-1;
  }	  
  
  $difference+=(12-$mnthComp);
 }
 
 $MnthCtr = 0;
 $dteCheck = 0;
 $dteDifference = 0;
 $currMnth = $Later_MnthComp;
 
 //Add the days for each month within the period
 while($difference>=0)
 {
  $theMnth = date('F',strtotime($yr."-".$currMnth));
  
  if($dteCheck==0)
  {
   if($theMnth==$Later_Mnth)
   {
	if($Later_Yr!=$yr)
	{
	 $dteDiff = ($Later_Day-0);
	 $dteCheck = 1;
	}
	else
	{
	 $dteDiff = ($Later_Day-$dteComp);
	 $dteCheck = 1;
	}
   }
   else
   {
	$dteDiff = ($Later_Day-$dteComp);
	$dteCheck = 1;
   }
  }
  else
  {
	 if(($theMnth=='April')||($theMnth=='June')||($theMnth=='September')||($theMnth=='November'))
	 {
	  $maxdays=30;
	 }
	 elseif($theMnth=='February')
	 {
	  ($chkLyr%4==0)? $maxdays=29 : $maxdays=28;
	 }
	 else
	 {
	  $maxdays=31;
	 }
	 
	 if(($theMnth==$Later_Mnth)&&($Later_Yr!=$yr))

	 {
	  $dteDiff = ($maxdays-$dteComp);
	 }
	 else
	 {
	  $dteDiff = ($maxdays-0);
	 }
  }
  
  $dteDifference+=$dteDiff;
  
  $MnthCtr+=1;
  $difference=$difference-1;

  ($currMnth==1)? $currMnth=12 : $currMnth-=1;
 }

//---------------------------------------------------------------------------------------------------------------------------------
 
?>

<html>
<title>Date Difference</title>
<body>
<div align="center" style="font-family:Arial; font-size:12px;">
<span style="font-size:18px; font-weight:bold;">
Get the Difference Between Two Dates
</span><br>
<span style="font-size:10px; color:#000099;">
(valid formats e.g. 12 Jul 2010, 07/12/2010, 2010-07-12, 12 July 2010)
</span><br>
<form method="post" action="date_compare.php">
<b>Earlier Date:</b><input type="text" name="Early_Date" value="<?=$Early_Date;?>"><br>

<b>Later Date:</b><input type="text" name="Later_Date" value="<?=$Later_Date;?>"><br>
<input type="submit" value="Submit">
</form><br>
<?
 echo "Difference in days: <b>$dteDifference</b><br>";
 //The month counter calculates the difference inclusively and must therefore be deducted by 1.
 echo "Difference in months: <b>".($MnthCtr-1)."</b><br>";
 echo "Difference in years: <b>$Yr_Diff</b><br>";
?>
<p style="font-size:10px; color:#000099;">By Shasha J. Munzara, email: shashamunzara@hotmail.com</p>
</div>
</body>
</html>


Re: PHP Date Comparison Code

Posted: Thu Jul 29, 2010 4:41 am
by mesotier

Re: PHP Date Comparison Code

Posted: Thu Jul 29, 2010 5:05 am
by Shasha Munzara
Only getting to see that one now. Oh well. But the one posted still works :roll: