Hello,
I created an events calendar which allows the user to add events. The events adding allows for daily, weekly, monthly (and yearly) events creation.
Well, everything works GREAT, except for this one little problem that I can't get my head around. when creating a repeating event that repeats on a certain weekday every x week of the month, the events are created, but every once in a while, whenever there's a 5th week in the previous month, the event for the following month ends up one week after it's supposed to.
I'm using strtotime and adding the repeat interval for each loop the event repeats. For example:
Let's say the user creates an event which will repeat every 1st Wednesday of each month for 6 months. The script will loop through for each repeat (6 times) and it is supposed to put the event into the database for each repeat, adding 1 month to the event date. It also (or it's supposed to) find the first day of the week in question each month and add x number of days to that week in order to determine the day of the event. Well, like I said, for some reason it doesn't do right when there are 5-week months.
Here's the actual code with some comments:
elseif($month_repeat_type == 'WDAY') {
$month = date('m', strtotime($startdate));
$year = date('Y', strtotime($startdate));
$month_begin = date('Y-m-d', mktime(0,0,0,$month,1,$year));
$rep_week = $month_weeknumber;
$rep_day = $month_day;
// each loop determines the day to repeat the event and adds to theDB
for($i=1;$i<=$occurrences;$i++) {
$int = $month_repeat_interval * $i;
$int = '+ '.$int.' month';
$newdate[$i] = strtotime($int, strtotime($month_begin));
$newdate[$i] = strtotime('+ '.$rep_week.' week', $newdate[$i]);
$newmonth[$i] = date('m', $newdate[$i]);
$newday[$i] = date('d', $newdate[$i]);
$newyear[$i] = date('Y', $newdate[$i]);
$newdate[$i] = findFirstDay($newmonth[$i], $newday[$i], $newyear[$i]);
$newdate[$i] = strtotime('+ '.$rep_day.' day', $newdate[$i]);
$data['event_date'] = date('Y-m-d', $newdate[$i]);
echo $data['event_date'].'<br /><br />';
//$dba->query_insert(MOD_CAL_EVENTS, $data);
$prevdate = $data['event_date'];
}
}
Any help would be appreciated; I'm sure it something small, but I don't see it.