Page 1 of 1

Recurring events on a calendar

Posted: Wed Aug 13, 2003 7:02 am
by JayBird
Hi,

Just found these forums, so i'd thought i'd ask a question that has been troubling me for a few weeks now.

I am currently developing an intranet for the company i work for, and all is going well.

There is a calendar/diary on there, which people can add events to, like "call client". All the info is stored in a MySQL database.

Some of these events occur on a monthly basis - this is where the problems start. It is easy enough, to get the event to show up every month, but if the following month is a saturday or sunday, i want the event to be shown on the monday.

Also, if i have a recurring event that first occured on the 31st of a month, this event will need to occur on the last working day of the next month.

Basically, on a monday, i need to check back 2 days to see if anything occurred over the weekend, but what if the monday is the 1st day of the month, and the previous month has 30 days, and i had an event sheduled that started on the 31st of a month??

Even typing this message is frying my brain. Is it actually this complicated, or am i missing something?

I dont want anyone to write the code for me, just want some ideas on the best way to acheive this.

Hope it all made sense.

Thanks in advance

Mark

Posted: Wed Aug 13, 2003 9:23 am
by McGruff
Add 28 to day of year, check if you're still in the same month and add another 7 if true, nothing if false?

Posted: Wed Aug 13, 2003 4:16 pm
by JayBird
Could you explain that a bit further, not sure why you would add 7

Posted: Wed Aug 13, 2003 6:47 pm
by McGruff
I think part of what you were asking boils down to setting a recurring monthly event on the nth day of the month. Let's say it's the first Monday each month.

If you add 28 to day of year then, in every month of the year except February (and maybe that too in a leap year), you might still be in the same month. So, check if month has changed and, if not, add another week to get the first Monday in the next month.

eg:

Monday, 4th Jan + 28 = Monday, 1st Feb
Monday, 1st Feb + 28 = Monday, 1st March OR Monday 29th Feb in a leap year

If you are working with timestamps that would be + 28*24*60*60 seconds.

To find the last working day (ie last Friday) in each month, you might want to use the date() function. With that, and a Unix timestamp, you can get the number of days in the month. Check the day name of the last day (date() fn again) and work backwards until you hit a Friday.

If you set something up where your script never sets events to occur during weekends you'll never have to check weekends for events and then move them back or forward to a working day - much tidier.

Hope that's given you some ideas.