IT & Programming

Get dates between 2 dates with PHP

  • How to get dates between 2 dates with PHP.
  • Get date range between two dates.
  • Show days between 2 dates.

This function accepts start date and end date parameter and returns all dates between them.

Function

function getDatesBetween2Dates($startTime, $endTime) {
	
	$day = 86400;
	$format = 'Y-m-d';
	$startTime = strtotime($startTime);
	$endTime = strtotime($endTime);
	$numDays = round(($endTime - $startTime) / $day) + 1;
	$days = array();
		
	for ($i = 0; $i < $numDays; $i++) {
		$days[] = date($format, ($startTime + ($i * $day)));
	}
		
	return $days;
}

Calling The Function

$days = getDatesBetween2Dates('2010/04/21', '2010/04/25');

foreach($days as $key => $value){

    echo $value . ', ';
}

Output

2010-04-21, 2010-04-22, 2010-04-23, 2010-04-24, 2010-04-25,

Comments

  1. Iam414 May 05, 2013

    Thanks.

  2. Jy January 01, 2013

    U best

  3. Simon November 11, 2012

    This is the code i have been looking for over mths

  4. Nilay Chaudhari October 10, 2012

    How to define 2010-04-21 instead $texfield

  5. Naren September 09, 2012

    It works but waht is $day = 86400;

  6. Chelle July 07, 2012

    Hi..how will i insert the values in my database

  7. Kirill Ozeritski June 06, 2012

    That's a good one. can u make a reverse one? :)

  8. Cindy Wiley November 11, 2011

    Add utc for dates so savings time is handled

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *