PHP calculate days between dates

15.Jul.2010

Very often we have a need to calculate the time passed between two dates. In order to make it easy, here is a snippet in PHP that can calculate days between dates.

PHP calculate days between dates snippet:

1
2
3
4
5
6
7
8
9
10
function calculate_days_between_dates($startDate, $endDate = null) {
	if(empty($endDate)) $endDate = date('Y-m-d');
	return abs(strtotime($endDate) - strtotime($startDate)) / (60*60*24);
}
 
//example of usage, and how to calculate days between date and today
echo calculate_days_between_dates('2010-07-14');
 
//example of usage, and how to calculate days between dates
echo calculate_days_between_dates('2010-07-14', '2010-07-20');