I am writing a script for an events calendar for one of several websites I manage (I know there are plenty of scripts out there, but it's easier for me to do this from scratch).

It has been requested of me to incorporate recurring events into the events creation page. I created a function to handle monthly, daily, and yearly recurrences and it seems to be fine. However, they also want one which allows them to do 'weekdays', like an event that occurs on Monday and Friday for a period of 4 weeks.

Here's the code for the actual events creation page (thus far):

<form method="POST" name="newevent" action="">
          <table width="90%" border="0">
          
          <tr>
          <th colspan="2" style="background-color: #0080C0; text-align: left; color: white;">
          Event Date and Time</th>
          </tr>
          <tr>
          <td width="25%">
          Date:
          </td>
          <td>
          <input type="text" name="startdate" size="12" id="startdatePicker" />
          </td></tr>
          <tr>
          <td>
          Start Time:
          </td>
          <td>
          <select name="starthour">
          <option value="">--</option>
          <?php
          for($h=1;$h<=12;$h++) {
               ?>
               <option value="<?php echo $h ?>"><?php echo $h ?></option>
               <?php
          }
          ?>
          </select>&nbsp;
          <select name="startminute">
          <option value="00">00</option>
          <?php
          for($m=01;$m<=59;$m++) {
               ?>
               <option value="<?php if(strlen($m) < 2) { echo '0'.$m; } else { echo $m; } ?>"><?php if(strlen($m) < 2) { echo '0'.$m; } else { echo $m; } ?></option>
               <?php
          }
          ?>
          </select>&nbsp;
          <select name="startampm">
          <option value="AM">AM</option>
          <option value="PM">PM</option>
          </select>
          </td></tr>
          <tr>
          <td>
          End Time:
          </td>
          <td>
          <select name="endhour">
          <option value="">--</option>
          <?php
          for($h=1;$h<=12;$h++) {
               ?>
               <option value="<?php echo $h ?>"><?php echo $h ?></option>
               <?php
          }
          ?>
          </select>&nbsp;
          <select name="endminute">
          <option value="00">00</option>
          <?php
          for($m=01;$m<=59;$m++) {
               ?>
               <option value="<?php if(strlen($m) < 2) { echo '0'.$m; } else { echo $m; } ?>"><?php if(strlen($m) < 2) { echo '0'.$m; } else { echo $m; } ?></option>
               <?php
          }
          ?>
          </select>&nbsp;
          <select name="endampm">
          <option value="AM">AM</option>
          <option value="PM">PM</option>
          </select>
          </td></tr>
          
          <tr>
          <th colspan="2" style="background-color: #0080C0; text-align: left; color: white;">
          Recurring
          </th>
          </tr>
          
          <tr>
          <td valign="top">
          Repeat Every:
          </td>
          <td>
          <select name="intervaltype" id="intervaltype" onchange="runJavas1()">
          <option value="N" selected="selected">None</option>
          <option value="D">Day</option>
          <option value="W">Week</option>
          <option value="M">Month</option>
          <option value="Y">Year</option>
          </select>&nbsp;
          for the next&nbsp;
          <input type="text" name="repeatinterval" size="3" maxlength="3" />&nbsp;
          <span id="selected_text">None</span><br /><br />
          </td>
          </tr>
          <tr>
          <td colspan="2" align="center">
          <b>OR</b>
          </tr>
          <tr>
          <td valign="top">
          Repeat Every:
          </td>
          <td>
          <select name="weekmonth" id="weekmonth" onchange="runJavas2()">
          <option value="N">None</option>
          <option value="W">Week</option>
          <option value="M">Month</option>
          </select>&nbsp;on<br />
          <blockquote>
          <input type="checkbox" name="sun" value="1" />&nbsp;Sunday&nbsp;&nbsp;
          <input type="checkbox" name="mon" value="1" />&nbsp;Monday&nbsp;&nbsp;
          <input type="checkbox" name="tue" value="1" />&nbsp;Tuesday&nbsp;&nbsp;
          <input type="checkbox" name="wed" value="1" />&nbsp;Wednesday<br />
          <input type="checkbox" name="thu" value="1" />&nbsp;Thursday&nbsp;&nbsp;
          <input type="checkbox" name="fri" value="1" />&nbsp;Friday&nbsp;&nbsp;
          <input type="checkbox" name="sat" value="1" />&nbsp;Saturday
          </blockquote>
          of the&nbsp;
          <select name="weekno">
          <option value="1">First</option>
          <option value="2">Second</option>
          <option value="3">Third</option>
          <option value="4">Fourth</option>
          <option value="5">Fifth</option>
          </select>&nbsp;week for&nbsp;
          <input type="text" name="totalweekmonth" size="3" maxlength="3" /> <span id="week_months">None</span>          
          </td>
          </tr>          
          
          <tr>
          <th colspan="2" style="background-color: #0080C0; text-align: left; color: white;">
          Event Details
          </th></tr>
          <tr>
          <td>
          Color:
          </td>
          <td>
          <input type="text" name="eventcolor" size="8" class="color {pickerPosition:'right', hash:true}" />
          </td>
          </tr>
          <tr>
          <td>
          Title:
          </td>
          <td>
          <input type="text" name="eventtitle" size="50" />
          </td></tr>
          <tr>
          <td valign="top">
          Description:
          </td>
          <td>
          <textarea name="eventdesc"></textarea>
          </td>
          </tr>
          
          <tr>
          <td>
          </td>
          <td>
          <input type="submit" name="createnewevent" value="  Save  " style="background-color: blue; color: white;" />
          </td>
          </tr>
          </table>
          </form>

The javascript (which you don't see) will only let the user select a normal recurring event OR a weekday recurring event. The function that I created to handle normal recurring events is:

function recurringEvents($type, $interval, $date) {

     $startdate = date('Y-m-d', strtotime($date));
     $day = explode('-', $startdate);
     $datetotime = mktime(0,0,0,$day[1], $day[2], $day[0]);

     $dates = array();
     
     //If interval type is daily
     if($type == 'D') {
          for($i=1;$i<$interval-1;$i++) {
               $newdate = '+ '. $i .' day';
               $dates[] = date('Y-m-d', strtotime($newdate, $datetotime));
          }
     }
     
     //If interval type is weekly
     if($type == 'W') {
          for($i=1;$i<$interval;$i++) {
               $newdate = '+ '. $i .' week';               
               $dates[] = date('Y-m-d', strtotime($newdate, $datetotime));
          }          
     }
     
     //If interval type is monthly
     if($type == 'M') {
          for($i=1;$i<$interval;$i++) {
               $newdate = '+ '. $i .' month';               
               $dates[] = date('Y-m-d', strtotime($newdate, $datetotime));
          }
     }
     
     //If interval type is yearly
     if($type == 'Y') {
          for($i=1;$i<$interval;$i++) {
               $newdate = '+ '. $i .' year';               
               $dates[] = date('Y-m-d', strtotime($newdate, $datetotime));
          }          
     }
     return $dates;     
}

But with regard to the weekday function...I am at a complete loss. I may be making this a harder issue that it needs to be (I've only been working on it a few hours), but outside help is greatly appreciated.

Member Avatar for diafol

date('w') gives the numerical representation of a day (0 = Sun...6 = Sat).

Say you want to recur a Tuesday from a date to another date.
Get the start date (get the day rep e.g. 10/2/11 = 4 (Thu)) and find the first occurrence of your day (say Weds = 3) after that. That's your first occurrence. Now just add '7' days to the previous occurrence with mktime(). As long as the date isn't greater than the end date.

Sound OK?

Right now I have the checkboxes with separate field names; would it be easier if I array these? I've gone back and forth on this (shoot, for that matter I could just loop one field 7 times if I array them)...I'll play with this tomorrow again and post any questions I have. I was thinking about date('w') but then I started reading a thread someplace (not here) about having to find the date of the first day of the week and going from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.