A little different approach for ya:
Code: Select all
<?php
function GetMeeting($whichWeek,$strFormat = 'l, F j, Y') {
static $aryMeetings = array();
$intNumMeetings = count($aryMeetings);
if ($intNumMeetings == 0) {
// First Time Call, build up an array of upcoming dates
// BEGIN: CONFIGURATION - Adjust these as needed
$intMeetingDayOfWeek = 4; // 0=Sun, 1=Mon, 2=Tues, 3=Wed, 4=Thur, 5=Fri, 6=Sat
$intNumMeetings = 3;
$intWeekOffset = 0; // Toggle this between 0 and 1 to find the correct set of weeks
// END: CONFIGURATION - Adjust these as needed
list($DayOfWeek,$intMonth,$intDay,$intYear) = explode("-",date('w-m-d-Y'));
$tsToday = mktime(0,0,0,$intMonth,$intDay,$intYear);
$intDayOffset = 60 * 60 * 25; // NOTE: 25 was used on purpose, see notes to why.
if ($DayOfWeek <= $intMeetingDayOfWeek) {
$intDaysTillThursday = ($intMeetingDayOfWeek-$DayOfWeek);
}
else {
$intDaysTillThursday = ((7+$intMeetingDayOfWeek)-$DayOfWeek);
}
$tsThurday = $tsToday + $intDayOffset * $intDaysTillThursday;
$WeekOfYear = date('W',$tsThurday);
if ($WeekOfYear % 2 == $intWeekOffset) {
// The meeting isn't this week, so skip to next week
$tsThurday += $intDayOffset * 7;
}
// We have first Thursday to use, loop through
for ($t = 0; $t < $intNumMeetings; $t++) {
// Becasue offsets adds one hour, lets get it back to midnight of the date
list($intMonth,$intDay,$intYear) = explode("-",date('m-d-Y',$tsThurday));
$tsThurday = mktime(0,0,0,$intMonth,$intDay,$intYear);
$aryMeetings[] = $tsThurday;
$tsThurday += $intDayOffset * 14; // Advance to the next Thursday
}
}
if ($whichWeek < $intNumMeetings) {
return date($strFormat,$aryMeetings[$whichWeek]);
}
else {
return '[ERR: Week Number out of Range]';
}
}
// END: function GetMeeting($whichWeek,$strFormat = 'l, F j, Y')
// LETS TEST IT OUT!
for($t=0;$t<5;$t++) {
echo $t,' = ',GetMeeting($t),"<br>\n";
}
?>
I broke it out a little more than if it was code I was using to make it a little more readable.
In the function, there are three lines that you need to adjust to configure when your meetings are. There are explained in the notes. The third one, leave alone at first, if you are geting the wrong weeks, change it to a 1
You may notice that in the code, to calculate a full day, I'm using 25 hours instead of 24. This was done on purpose to handle if the ending of Daylight Savings Time occurs sometime between now and end of the list of meetings (that day there is actually 25 hours that day if the timezone of the server is set to recognizes them). Since we don't care what hour of the day it is, technically if you add 5 days, to Midnight, August 8, 2011, you would get 5:00am, August 13, 2011, the code will round it back to midnight for ya. (This trick is safe, as long as you are never adding more than 22 days before rounding back to midnight (entering Daylight Savings time technically has just 23 hours that day).
To see an example of what I mean by the above, try changing it back to 24, and then adjust number of weeks to calculate (and display) to be 26 (to get a years worth) you would see something like this:
[text]4 = Thursday, October 13, 2011
5 = Thursday, October 27, 2011
6 = Wednesday, November 9, 2011
7 = Wednesday, November 23, 2011[/text]
This is because only using 24 hours a day, when it went to calculate Thursday, Nov 10, you actually ended up with 11:00pm, Wed, Nov 9th, which is obviously not a Thursday. Using the 25 hours for offset gets you 7:00am, Thurs, Nov 10th, which is the correct date. (and time gets reset back to midnight before being stored and using during next run through loop.)
I prefer this method over other functions, as there is less for for it to do when you are just working with integers instead of code that had to figure out what "next Thursday" means. As long as it is coded solid, works just fine.
-Greg