
I have been pulling out my hair for the last few hours... Given two dates in whichever format, e.g. 2009-10-06 and November 12, 2009, which is the best logical approach here to get all dates in between those two dates? I was thinking of converting the received dates into timestamps, then using mktime to add one day to the older timestamp till i get to the new one, but a small voice tells me there is a better way. Any ideas? Here is the code I am using. <?php /** * Get the days between two dates and return those actual dates in an array */ function getDaysBetweenDates($startDate, $endDate){ //initialize array that will get returned $daysBetweenDatesArray = array(); //attempt to convert the received dates into timestamps $startDateTimestamp = strtotime($startDate); $endDateTimestamp = strtotime($endDate); /*TODO: Problem 1: even if the received dates are bogus, strtotime() will convert them into sane timestamps, which might pass the checkdate() validation below */ //check that the received values are valid if (!checkdate(date('n',$startDateTimestamp), date('j',$startDateTimestamp), date('Y',$startDateTimestamp))) { //do some error handling here return false; } if (!checkdate(date('n',$endDateTimestamp), date('j',$endDateTimestamp), date('Y',$endDateTimestamp))) { //do some error handling here return false; } //dates received are probably valid /* Logic 1: One day has (24*60*60) seconds, therefore, if I can get the number of 86400 seconds between those two days, I can get those actual dates. Havent used this method Logic 2: Use mktime to add One to the start date, then increment $startDateTimestamp with 86400, i.e. the number of seconds in one day and loop it untill $startDateTimestamp is less than or equal to $endDateTimestamp. */ //variable to count the number of times the loop has run, will be used as index in the $daysBetweenDatesArray array $loopCounter = 1; //variable to store the dates found in between, not really necessary, the dates found could be put directly in the array $dateInBetweenTimestamp = ''; //assume startDate is included, put it as first element in array $daysBetweenDatesArray[0] = date('j-n-Y', $startDateTimestamp); while ($startDateTimestamp <= $endDateTimestamp) { //in order to use mktime, we will need seconds, minutes, hours, day, month and year for each date $startDateSecond = date('s',$startDateTimestamp); $startDateMinute = date('i',$startDateTimestamp); $startDateHour = date('G',$startDateTimestamp); $startDateDay = date('j',$startDateTimestamp); $startDateMonth = date('n',$startDateTimestamp); $startDateYear = date('Y',$startDateTimestamp); $dateInBetweenTimestamp = mktime($startDateHour, $startDateMinute, $startDateSecond, $startDateMonth, $startDateDay+1, $startDateYear); $daysBetweenDatesArray[$loopCounter] = date('j-n-Y', $dateInBetweenTimestamp); //increment loop counter $loopCounter++; //increment $startDateTimestamp $startDateTimestamp = $startDateTimestamp + 86400; } return $daysBetweenDatesArray; } ?> How to use: <?php $datesInBetween = getDaysBetweenDates('2009-01-01', '2009-01-30'); print_r($datesInBetween) ; ?>