ProAnswers.org

How to calculate difference betn two Dates using PHP?

I have two dates of the form:

Start Date: 2010-03-18 End Date: 2011-06-24
Now I need to find the difference between these two in the below form:

1 years, 3 months and 6 days
How can I do this in PHP?

You can use strtotime() to convert two dates to unix time and then calculate the number of seconds between them. From this it’s rather easy to calculate different time periods.

			$date1 = "2010-03-18"; 
			$date2 = "2011-06-24"; 
			 
			$diff = abs(strtotime($date2) - strtotime($date1)); 
			 
			$years = floor($diff / (365*60*60*24)); 
			$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); 
			$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); 
			 
			printf("%d years, %d months, %d days

", $years, $months, $days);