Tim Tim - 17 days ago 9
PHP Question

Using PHP DateInterval to create recurring events

So I have spent quite a bit of time researching how best to add recurring events to my calendar application.

I would like to use the PHP DateInterval function and have formulated the code below to try and work out how to create a recurring event based on the original events

Start Date
,
Finish Date
and the
EndDate of Recurrence
.

//user defined event start and finish dates
$eventStart = new DateTime( '2011-01-31 09:00:00' );
$eventFinish = new DateTime( '2011-01-32 17:00:00' );

//user defined event recurring end date
$endRecurring = new DateTime( '2011-05-31 23:59:59' );

//define for recurring period function
$begin = $eventStart;
$end = $endRecurring;

//define our interval
$interval = DateInterval::createFromDateString('next friday');
$period = new DatePeriod($begin, $interval, $end, DatePeriod::EXCLUDE_START_DATE);

//loop through and create new dates for recurring events
foreach ( $period as $dt )
$recurringStartDate = $dt->format( "l Y-m-d H:i:s\n" );
$recurringEndDate = ?NOT SURE HOW TO PROCESS THE END DATE IN THIS START DATE FOREACH LOOP?


This should hopefully create a list of new event start dates. BUT I also need to define new end dates for my recurring events. How do I do this? Do I need to process this in the event start date foreach loop?

My other question is how I could combine multiple dateIntervals to take care of
Repeat every Monday, Wednesday and Friday
? Currently only single dateIntervals are working like
next friday


Thanks for your help!

Tim

Answer

There is a Date/Calendar recursion library for PHP 5.2+ by Thomas Planer. Its not DateInterval, but it seems to do the trick. Check it out at https://github.com/tplaner/When

I know this is an old question, but perhaps someone will benefit!

Comments