Recurring events on a calendar

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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Recurring events on a calendar

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Could you explain that a bit further, not sure why you would add 7
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Post Reply