5 || $nthDay < 1) { return "Not a valid date."; } if ($dayNum < 0 || $dayNum > 6) { return "Not a valid day."; } $thisMonth = date("n"); // [m]m $thisYear = date("Y"); // YYYY // This Month $lastDate = testMonth ($thisMonth, $thisYear, $nthDay, $dayNum); if (date("n/j/Y") == date("n/j/Y", $lastDate)) { return $meetsTodayStr; } // Returns Meets Today message event is today. // Next Month if (! $lastDate || $lastDate < time()) { if ($thisMonth != 12) { $thisMonth++; } else { $thisMonth = 1; $thisYear++; } $lastDate = testMonth ($thisMonth, $thisYear, $nthDay, $dayNum); } if (! $lastDate) { return $noDateStr; } // If the day specified in the function call does not occur within the two months tested. else { return date($dateFormatStr, $lastDate); } // Otherwise, return the date of the next event. } // Modularizes the code. This function looks for a future date in the month specified. You can alter the functionality of this algorithm by manipulating the way this function is called in the main part of this script. function testMonth ($thisMonth, $thisYear, $nthDay, $dayNum) { $daysOfWeekFound = 0; $dayOfMonth = 1; $daysInMonth = daysInMonth($thisMonth, $thisYear); while ($daysOfWeekFound < $nthDay && $dayOfMonth <= $daysInMonth) { $testDateStr = "$thisMonth/$dayOfMonth/$thisYear"; // String dd/mm/yyyy must be converted to timestamp. //Last date is a timestamp integer that contains the last date tested. $lastDate = strtotime($testDateStr); $lastDateArr = getdate($lastDate); if ($lastDateArr[wday] == $dayNum) { $daysOfWeekFound++; } $dayOfMonth++; } if ($daysOfWeekFound != $nthDay) { return false; } return $lastDate; } // How many days are in a given month. function daysInMonth($month, $year) { $feb = daysInFeb($year); $daysInMonthsArr = array(31, $feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); $monthIndex = $month -1; return $daysInMonthsArr[$monthIndex]; } // Deals with the leap year issue. // Not Y2.2k compliant. function daysInFeb($year) { if (is_int($year / 4) && ($year != 1900 && $year != 2100) ) { return 29; } else { return 28; } } ?>