dadamssg87 Posted May 3, 2011 Share Posted May 3, 2011 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 <?php $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. Quote Link to comment https://forums.phpfreaks.com/topic/235444-get-list-of-dates-between-a-date-range/ Share on other sites More sharing options...
cunoodle2 Posted May 3, 2011 Share Posted May 3, 2011 <?php 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.. http://edrackham.com/php/get-days-between-two-dates-using-php/ Quote Link to comment https://forums.phpfreaks.com/topic/235444-get-list-of-dates-between-a-date-range/#findComment-1210035 Share on other sites More sharing options...
Psycho Posted May 3, 2011 Share Posted May 3, 2011 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'); print_r($dateRange); Output: Array ( [0] => 2011-05-03 [1] => 2011-05-04 [2] => 2011-05-05 [3] => 2011-05-06 [4] => 2011-05-07 [5] => 2011-05-08 ) Quote Link to comment https://forums.phpfreaks.com/topic/235444-get-list-of-dates-between-a-date-range/#findComment-1210048 Share on other sites More sharing options...
dadamssg87 Posted May 3, 2011 Author Share Posted May 3, 2011 wow...awesome, thanks Quote Link to comment https://forums.phpfreaks.com/topic/235444-get-list-of-dates-between-a-date-range/#findComment-1210050 Share on other sites More sharing options...
Psycho Posted May 3, 2011 Share Posted May 3, 2011 @cunoodle2: 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. Quote Link to comment https://forums.phpfreaks.com/topic/235444-get-list-of-dates-between-a-date-range/#findComment-1210053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.