0
votes

I have created a PHP script to read an ical file from my mailserver. This script is used to plan events in my PBX so when people call and I'm out of office it automatically redirects them to voicemail. This works perfectly for one time events, but I would like it to work for recurring events.

This is the result of the script:

[BEGIN] => VEVENT
[DTSTAMP] => 20170920T120736Z
[UID] => 80462778A326E04EBD831336D01F2A2F179EBCBCC8BD7A45954DE9CF
[DESCRIPTION] =>  \n
[PRIORITY] => 5
[SUMMARY] => Summary
[CLASS] => PUBLIC
[LOCATION] => Place
[X-ALARM-TRIGGER] => -PT30M
[DTSTART] => 20170912T083000
[DTEND] => 20170912T173000
[RRULE] => FREQ=WEEKLY;BYDAY=TU
[END] => VEVENT

I can recognize a recurring event by the "RRULE" parameter, but thinking it through the script would have to calculate the recurring date and that would take a lot of CPU I guess. Especially since my ical file already has over 1800 events and these would all have to be checked. Then there is also the question on how to check this, because recurring events can be daily, weekly, monthly, yearly, and that is without intervals, e.g. every other week.

Any idea on how to go about this?

1

1 Answers

1
votes

Give something like this a try

<?php

$recur = 'FREQ=WEEKLY;BYDAY=TU';
$ex = explode(';', $recur);
$freq = str_replace('FREQ=','',$ex[0]);
$day = str_replace('BYDAY=','',$ex[1]);

$dates = [];

switch ($freq) {
    case 'WEEKLY':
        $date = new DateTime(); // set to correct day obvs
        $dates[] = $date;
        for ($x = 0; $x <= 52; $x ++) {
            $date = clone $date;
            $date->modify('+1 week');
            $dates[] = $date;
        }
        break;
}

foreach($dates as $date) {
    echo $date->format('Y-m-d')."\n";
}

You'll need to code the rest yourself, but this generates a bunch of dates one week apart from each other.

See it working here https://3v4l.org/Dgriv