Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with function to loop through days

Status
Not open for further replies.

ecprogrammer

Programmer
Dec 1, 2011
1
I need a function where I pass the startdate ,enddate, and dayof week and it returns me an array of all the days in between on that day of the week


can someone help me with this
 
you do not specify the version of php you are using. so here is a suboptimal solution that should work on most versions

Code:
<?php
function weekIntervals($start = "2010-01-01", $end=null, $day="Monday"){
	if (is_null($end)) $end = date('Y-m-d');
	$uEnd = strtotime($end);
	$return = array();
	$uStamp = strtotime($start);
	while($uStamp <= $uEnd):
		$uStamp = strtotime('next ' . $day, $uStamp);
		if($uStamp <= $uEnd) $return[] = date('Y-m-d', $uStamp);
	endwhile;
	return  $return;
}
date_default_timezone_set('UTC');
print_r(weekIntervals('1 January 2005', '28 February 2009', 'Sunday'));
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top