Date Difference Calculation Code (Revised)
Posted: Thu Aug 05, 2010 2:20 am
In the event that you get the error message "call to undefined function DateTime::diff" when using the date difference calculation code from "http://php.net/datetime.diff," the code below can be used to calculate the differences between any 2 dates as from 01 Jan 1902. It can be altered and saved as an include file or function. To test it, copy & Save it as "date_compare.php" then run it as it is
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."-01"));
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;
if($currMnth==1)
{
$currMnth=12;$chkLyr=$chkLyr-1;
}
else
{
$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>