Set a repeating date on 1st, 2nd etc... Monday, Tuesday...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeffeyp
Forum Newbie
Posts: 5
Joined: Thu Mar 27, 2008 4:23 pm

Set a repeating date on 1st, 2nd etc... Monday, Tuesday...

Post by jeffeyp »

I need to be able to reset an event date based on the day of the month:
1st, 2nd, 3rd ...
Monday, Tuesday, Wednesday...

I found a post here: http://www.experts-exchange.com/Databas ... 76400.html that is using Server 2000 (TSQL).

I am using PHP and MySQL.

Any help/suggestions are greatly appreciated!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Set a repeating date on 1st, 2nd etc... Monday, Tuesday...

Post by califdon »

Here's an include file I wrote that might give you what you are looking for. If it does, you're welcome to use it:

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);
}
?>
Post Reply