Jump to content

get list of dates between a date range


Recommended Posts

I'm trying to wrap my head around how i should go about doing this. I have two dates...2011-05-03 and 2011-05-08. I want to write a function that creates an array of the days that consist of that date range. So say something like


$start = "2011-05-03";
$end = "2011-05-08";

function get_days($start, $end)
//code to get the days

echo get_days($start, $end);

//hopefully produce something like...
$day['1'] = "2011-05-03";
$day['2'] = "2011-05-04";
$day['3'] = "2011-05-05";
$day['4'] = "2011-05-06";
$day['5'] = "2011-05-07";
$day['6'] = "2011-05-08";


anybody know any idea how to do something like this? Pretty sure i'm going to need the mktime() function to account for ranges going across months and years. 

Link to comment
Share on other sites

function GetDays($sStartDate, $sEndDate){  
// Firstly, format the provided dates.  
// This function works best with YYYY-MM-DD  
// but other date formats will work thanks  
// to strtotime().  
$sStartDate = gmdate("Y-m-d", strtotime($sStartDate));  
$sEndDate = gmdate("Y-m-d", strtotime($sEndDate));  

// Start the variable off with the start date  
$aDays[] = $sStartDate;  

// Set a 'temp' variable, sCurrentDate, with  
// the start date - before beginning the loop  
$sCurrentDate = $sStartDate;  

// While the current date is less than the end date  
while($sCurrentDate < $sEndDate){  
// Add a day to the current date  
$sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));  

// Add this new day to the aDays array  
$aDays[] = $sCurrentDate;  

// Once the loop has finished, return the  
// array of days.  
return $aDays;  


I just did a google search for.. "php function get all dates between"


And I got this for the first result..


Link to comment
Share on other sites

I thought I had a function like this before, but can't find it. I found something similar and modified it accordingly. It takes an optional third parameter in case you want the date format differently.


function getDateRange($startDate, $endDate, $format="Y-m-d")
    //Create output variable
    $datesArray = array();
    //Calculate number of days in the range
    $total_days = round(abs(strtotime($endDate) - strtotime($startDate)) / 86400, 0) + 1;
    if($days<0) { return false; }
    //Populate array of weekdays and counts
    for($day=0; $day<$total_days; $day++)
        $datesArray[] = date($format, strtotime("{$startDate} + {$day} days"));
    //Return results array
    return $datesArray;

$dateRange = getDateRange('2011-05-03', '2011-05-08');




    [0] => 2011-05-03
    [1] => 2011-05-04
    [2] => 2011-05-05
    [3] => 2011-05-06
    [4] => 2011-05-07
    [5] => 2011-05-08

Link to comment
Share on other sites



You should be careful when creating loops such as this.

while($sCurrentDate < $sEndDate){


The OP didn't state "where" these dates would be coming from but they could be coming from user input. If so, the user could accidentally input the dates backwards. In that case (or if the programmer made a mistake) you would have an infinite loop.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.