NYCPHP Meetup

NYPHP.org

[nycphp-talk] Recurring Monthly Calculations

Daniel Kushner kushner at gmail.com
Fri Jun 25 13:33:09 EDT 2004


just hacked this. Needs some error checking before date calculation.
The function signiture is a little different from what you asked ;)
You can get rid of the two array definitions with some more PHP.


function recurMonthlyByDay($dayOfWeek, $week, $startYear, $startMonth, $count) {
	$daysArray = array(	1 => 'Monday', 
			2 => 'Tuesday', 
			3 => 'Wednesday',
			4 => 'Thursday',
			5 => 'Friday',
			6 => 'Saturday',
			7 => 'Sunday');
	$weeksArray = array(1 => 'First',
			2 => 'Second',
			3 => 'Third',
			4 => 'Fourth');
	$result = array();
	for($i = 0; $i < $count; ++$i) {
		$result[] = strtotime("$weeksArray[$week] $daysArray[$dayOfWeek]",
mktime(0,0,0,$startMonth++, 1, $startYear));
	}
	return $result;
}

$dates = recurMonthlyByDay(3, 4, 2004, 5, 23);
foreach ($dates as $date) {
	echo date("l \\t\h\e jS, M Y", $date);
	echo "\n";	
}



On Fri, 25 Jun 2004 09:11:26 -0700, Hans Zaunere <hans not junk at nyphp.com> wrote:
> 
> 
> Hey all,
> 
> I needed to write a function that will tell me the dates of future
> events that recurr monthly by day of month.  So for instance, "4th
> Tuesday of the month for the next 6 months"
> 
> It ended up to be more difficult than I thought and I wanted to run the
> function past you guys.  See attached.  Am I missing some clever way of
> doing this?  The function seems to work, but another set of eyes would
> be helpful - and it depends on UNIX timestamps, so...
> 
> The function is called as:
> 
> recurMonthlyByDay(2,4,20030522,23);
> 
> Which would return an array listing the 4th Tuesday, starting in May of
> 2003, for the next 23 months.  If you specify the second argument to be
> greater than 5, then the last Tuesday would always be returned.
> 
> Thanks guys... feel free to use the function, too (that is, if it works)
> 
> H
> 
> 
> 
> 
> RecurMonthlyByDay.inc - 1K
> noname - 1K Download 
>



More information about the talk mailing list