Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Caliga's avatar

Calendar - Recurring Events

Hello there!

This is going to be a little hard to explain, so I apologise in advance for confusing anyone and/or migraines caused.

I'm currently in the process of coding an events module for use in our custom Laravel CMS.

After doing some research into the different ways to handle event recursion I decided the most sensible way to do it was the following:

I have an events table, which stores a "Master" event, if the event doesn't recur this is essentially a single event that shall appear in the calender and no further work must be done. If this is a recurring event, it is essentially used as a template for all future events of it's type. These kinds of events use a CRON expression to define when those future events will occur, for example I can specify 0 6 * * *, which in human terms states "Repeat every day, at 06:00". To parse this string I am using the following library, which I have modified to be able to get all instances of an event between two dates

https://github.com/mtdowling/cron-expression

There are two related tables for each event, known as exceptions and materializations, the former is simply a record of any instances of an event that have been deleted, the latter is a record of any instances of an event that have been modified, containing those modifications. They simply store a reference to the Master event, and a date, so that when the instances of an event are calculated from the CRON expression, their date can be compared to the eager loaded exceptions and materializations data, to see if there is a match. This way we know, for example, the occurrence of Event X, on 2015-3-28 06:00:00 has been cancelled or modified.

This works absolutely fine for viewing an individual event, as there are very few cases where you would want to view any more than one month at a time, so we only need to calculate the instances of that event between, for example, January and February.

The problem comes with displaying multiple events. It's pretty standard practice to have an overview calendar, displaying all events that are on in a month. To display this data we would need to check every single master event in the database (a they have no end date to them), to see if any of it's iterations fall into that particular timeframe, and we would then need to look at every applicable exception and materialization. So far the calculation time for a mere 7 records is over 5 seconds, which is unacceptable.

I know there must be a logical solution to the problem, as the method I have described to you is nearly identical to the iCal standard, I have considered methods to cache the results but it seems a little pointless doing so as the idea of using this method was to avoid storing instances in the first place.

Do any of you have experience with a similar problem? How did you overcome it? Your help would be greatly appreciated!

Thanks

0 likes
7 replies
gcwilliams's avatar

If your working with recurring events you might find this package helpful:

https://github.com/simshaun/recurr

While not specific to Laravel, it can make recurrence easier to manage and shouldn't be super hard to integrate into a Laravel site.

1 like

Please or to participate in this conversation.