Maybe you need three tables
- event_base
- with event, name, location, duration, and details, etc.
- event_instance
- For actual instances of reoccurring events
- only create instances when the user tries to view that month/week/day, etc.
- only create instances if an instance doesn't exist at that date
- have an original date and current date (in case this instance can be moved around)
- event_reoccurrence table
- with an end date field. if end date is null, there is no end date.
Here is a sample event_instance table schema
- event_base_id
- event_reoccurrence_id (null if no reoccurrence) The rule that created the event.
- originally_created_for_date (if user moves the instance it won't get re-added to the original date)
- current_date (the date where it shows up on the calendar
- location_override
- duration_override
- other overrides, etc.
Here is a sample event_reoccurrence table schema (reoccurrence rules)
- event_reoccurrence_id
- event_base_id (unsigned int)
- day_of_week varchar(20)
- day_of_month varchar(20)
- day_of_year varchar(20)
- week_of_month varchar(20)
- week_of_year varchar(20)
- months_of_year varchar(20)
- years varchar(20)
- lastdate_of_reoccurrence Date
Values in #2 - #8 can be a single number, a range "2-6", numbers seperated by commas or null.
- null means irrelevant (event can happen regardless of this value)
- some number: event happens on a date when that date matches the number
- weekday: 5 = event can not happen on days other than friday
- numbers seperated by commas: event happens when any of the numbers matches now
- month: 2, 4, 5 = event can happen in February, April and May
- range: event happens between the range
- days of month: 1-12 = event can happend from the 1st to the 12th of each month
- When every non null field matches, a date the event happens on that date.
So if you check a date and every field is null or it matches that date, then the event happens on that date. Perhaps you could have multiple entries per event to build complex patterns.
Week of month would be first 7 days, not first Sunday. If week_of_month was 3 and day of week was 2, and everything else was null except event ID, it's like the third Tuesday of every month.
Then as the person views the calendar, you request all the reoccurring events against each day of that month and create actual events for each one that matches