AC Computing Logo AC Computing Banner
     
     
     

The Following class can be used be used to return the difference between to dates in days, simple arthmitic can then convert this to weeks, month or years

Syntax

dateDiff(largeDateString, smallDateString)
 

Considerations

Date Format "YYYY-MM-DD"

 

To use this script simply copy and paste into a file for example create a new file called dateDifference.php and paste the code in, to use this file you will need a knowledge of php classes


<?PHP
#####################################################################
#																	#
#    This class is provided as is, it is wrote by David Dripps and  #
#    is in verision one.  It will take two dates and return the     #
#    difference in them in days, the large date must come first     #
#    comments and suggestions and improvments can be sent to        #
#    daviddripps@accomputing.biz                                    #
#                                                                	#
#   Licensed under creative commons 								#
#	http://creativecommons.org/licenses/by/2.0/uk/					#
#   This means you must mention me on the credits of any program	#
#	or website that uses this class									#
#																	#
#####################################################################

									
class dateDiffInDays{

	 function dateDiff($largeDate, $smallDate)
	{

		$l = explode ('-', $largeDate);
		$s = explode ('-', $smallDate);
		$days;
		$months;
		$years;
	
		
		//Starting from right hand side with days
		
			if ($l[2] - $s[2] >= 0 )
				{
				//if you can subtract the day in large from small
					$days = $l[2] - $s[2];
				}

			if ($l[2] - $s[2] < 0)
			{
			//if you can't bull a month off the smaller date and carry it over
	
			$l = pullOffMonth($l);
		
			
			$days = $l[2] - $s[2];
	
			}
		
		//Moving accross to months
			if ($l[1] - $s[1] >= 0 )
				{
				//if you can subtract the month in large from small
					$months = $l[1] - $s[1];
				}

			if ($l[2] - $s[2] < 0)
			{
			//if you can't pull a year off the larger date and carry it over
	
			$l = pullOffYear($l);
	

			$months = $l[1] - $s[1];
	
			}
		
			//count the months in
			for ($count = 0; $count <= $months; $count++)
			{
				$days += daysInMonth($months);
			
				$months = $months -1;
			
			
		
			}
		
		//Finally the year
				if ($l[0] - $s[0] >= 0 )
				{
				//if you can subtract the month in large from small
					$years = $l[0] - $s[0];
				

				
					//add in years
					$days += $years * 365;
				}	


		return $days;

	}

	 function pullOffMonth($date)
	{

	
		//if not January
		if ($date [1] > 1)
		{
			//how many days are in the month before current
			$daysToAdd = daysInMonth($date[1]-1);

			//take a month off
			$date[1] = $date[1]-1;
		
		
			// add the number of days
			$date[2] = $date[2] + $daysToAdd;
		
		}
	
		// if it's january we need to pull of a year too
		if ($date [1] == 1)
		{
			//Take a year off
			$date = pullOffYear($date);
		
			//how many days are in the month before current
			$daysToAdd = daysInMonth($date[1]-1);
		
			//take a month off
			$date[1] = $date[1]-1;
		
		
			// add the number of days
			$date[2] = $date[2] + $daysToAdd;
	
		}
	
		return $date;
	
	}

	 function pullOffYear ($date)
	{
		$year = $date[0]-1;
		$month = $date[1] +12;
	
		$date[0] = $year;
		$date[1] = $month;
	
		return $date;
	
	}

	 function daysInMonth($numericValueOfMonth)
	{
		$numberOfDays;
		switch ($numericValueOfMonth) {

		case 1: $numberOfDays = 31; break; //Jan
		case 2: $numberOfDays = 28; break; //Feb
	   	case 3: $numberOfDays = 31; break; //March
	   	case 4: $numberOfDays = 30; break; //April
	   	case 5: $numberOfDays = 31; break; //May
	   	case 6: $numberOfDays = 30; break; //June
	   	case 7: $numberOfDays = 31; break; //July
	   	case 8: $numberOfDays = 31; break; //August
	   	case 9: $numberOfDays = 30; break; //September
	   	case 10: $numberOfDays = 31; break; //October
	   	case 11: $numberOfDays = 30; break; //November
	   	case 12: $numberOfDays = 31; break; //Decemeber
	   	default: $numberOfDays = 0;
		}
	
		return $numberOfDays;
	}

	function inc($num)
	{
		$number = $num +1;
		return $number;
	}
}

?>