Thursday, July 29, 2010

get consecutive dates between any two dates

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)); //Y-m-d
// Start the variable off with the start date
//$aDays[] = $sStartDate;
$ex = explode("-",$sStartDate);

$aDays[] = $ex[1] ."/" .$ex[2] . "/" . $ex[0];

// 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
$ex = explode("-",$sCurrentDate);

$aDays[] = $ex[1] ."/" .$ex[2] . "/" . $ex[0]; //$sCurrentDate;
}

for ($i=0;$i<4;$i++){

// print_r($aDays[$i]);

}

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

No comments: