I can't figure out what you had in mind as an algorithm. In the past, I've used this generic algorithm as an include file, which can find the date of the 1st, 2nd, 3rd, 4th or 5th occurrence of any day of the week, in a particular month and year:
Code: Select all
<?php
function nextdate($ordinal, $dow, $next) {
// Returns the date of next occurence of the [ordinal] day of the [next] month--
// [ordinal being the 1st, 2nd, 3rd, 4th or 5th
// day being Sun - Sat, 0 - 6
// next being either the next occurrence (0), the following occurrence (1), etc.
// Example: to find the next 2nd Friday after today's date,
// nextdate(2,5,0)
// Validate parameters:
if ($ordinal < 1 | $ordinal > 5 | $dow < 0 | $dow > 6) return 0;
if ($next=='') $next=0;
$ordinal--;
// First, calculate the next occurrence:
$month=date('m'); // this month
$year=date('Y'); // this year
$dt1=mktime(0,0,0,$month,1,$year); // the 1st of this month
$dow1=date('w',$dt1); // day of the week of the 1st of this month
while ($dow1<>$dow) { // increment days until 1st dow of this month
$dt1=$dt1 + 24*60*60;
$dow1=date('w',$dt1);
}
$dt2=$dt1 + $ordinal*7*24*60*60; // advance one week for desired occurrence
if (mktime() > $dt2) { // we've already passed the second Friday of this month!
$nextmonth=$dt2 + 30*24*60*60;
$month=date('m',$nextmonth); // next month
$year=date('Y',$nextmonth); // next year
$dt1=mktime(0,0,0,$month,1,$year); // the 1st of next month
$dow1=date('w',$dt1); // day of the week of the 1st of next month
while ($dow1<>$dow) { // increment days until 1st dow of next month
$dt1=$dt1 + 24*60*60;
$dow1=date('w',$dt1);
}
}
$dt2=$dt1 + $ordinal*7*24*60*60; // advance one week for desired occurrence
// Now $dt2 contains the timestamp of the next meeting date
// If a following occurence was requested, we now have to advance the month:
if ($next > 0) {
$dt2 += $next*30*24*60*60;
// And we once again must find the dow for the first of that month:
$month=date('m',$dt2); // next month
$year=date('Y',$dt2); // next year
$dt1=mktime(0,0,0,$month,1,$year); // the 1st of next month
$dow1=date('w',$dt1); // day of the week of the 1st of next month
while ($dow1<>$dow) { // increment days until 1st dow of next month
$dt1=$dt1 + 24*60*60;
$dow1=date('w',$dt1);
}
$dt2=$dt1 + $ordinal*7*24*60*60; // advance one week for desired occurrence
}
return $dtformat=date('l, M j, Y',$dt2);
}
?>
You're welcome to use it or modify it to your own needs. No warranty!
