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>
<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>
<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>
<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>
<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>
for the next
<input type="text" name="repeatinterval" size="3" maxlength="3" />
<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> on<br />
<blockquote>
<input type="checkbox" name="sun" value="1" /> Sunday
<input type="checkbox" name="mon" value="1" /> Monday
<input type="checkbox" name="tue" value="1" /> Tuesday
<input type="checkbox" name="wed" value="1" /> Wednesday<br />
<input type="checkbox" name="thu" value="1" /> Thursday
<input type="checkbox" name="fri" value="1" /> Friday
<input type="checkbox" name="sat" value="1" /> Saturday
</blockquote>
of the
<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> week for
<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.