Pregunta

What I Am Trying!

I am making an event calendar using jquery, php and mysql. The events created are stored in mysql database and then fetched back and displayed in the calendar. The events contain a start and end date. In case where user wants to create an event which repeats through out say a week, then he can use the recurring property of the calendar and set the recurring end date to one week from event start.

Where I Am Blocked At!

Suppose a user requires a recurring event which dosenot end like birthdays or a weekly event which repeats every week. In this case I think inserting each event into the database for many years is not a good approach. Lets take another case where user creates daily repeating events for 2 years. Here the database will become very populated.

What I Am Looking For!

Can someone provide me some idea on how to manage repeating events with no end dates or with very long end dates.

¿Fue útil?

Solución

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

  1. event_base_id
  2. event_reoccurrence_id (null if no reoccurrence) The rule that created the event.
  3. originally_created_for_date (if user moves the instance it won't get re-added to the original date)
  4. current_date (the date where it shows up on the calendar
  5. location_override
  6. duration_override
  7. other overrides, etc.

Here is a sample event_reoccurrence table schema (reoccurrence rules)

  1. event_reoccurrence_id
  2. event_base_id (unsigned int)
  3. day_of_week varchar(20)
  4. day_of_month varchar(20)
  5. day_of_year varchar(20)
  6. week_of_month varchar(20)
  7. week_of_year varchar(20)
  8. months_of_year varchar(20)
  9. years varchar(20)
  10. 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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top