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
To begin, go to Add Event and click the event date to expand the options. Click Repeat Event to add a recurrence rule. A recurring event could occur every week on the same day, for one example. If you're going on holiday for a week, add an exception to the recurrence.
A common way to create a DateInterval object is by calculating the difference between two date/time objects through DateTimeInterface::diff(). Since there is no well defined way to compare date intervals, DateInterval instances are incomparable.
Add a recurrence domain to the database that supports a number of different values, including “daily”, “weekly”, and “monthly”. Add a recurrence column to the events table that identify how an event recurs. Add a recurrence_dates table that contains a pre-generated list of recurrences for a given date.
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.
Hi I had to develop something similar and i did the flowing:
$event_date = "2012-10-06";
$event_end_date = "2012-10-08";
$event_repetition_type = "Daily";
$date_calculation = "";
switch ($event_repetition_type) {
case "Daily":
$date_calculation = " +1 day";
break;
case "Weekly":
$date_calculation = " +1 week";
break;
case "Monthly":
$date_calculation = " +1 month";
break;
default:
$date_calculation = "none";
}
$dateArray[] = $event_date;
$day = strtotime($event_date);
$to = strtotime($event_end_date);
while( $day <= $to )
{
$day = strtotime(date("Y-m-d", $day) . $date_calculation);
$dateArray[] = date("Y-m-d" , $day);
}
//here make above array as key in $a array
$a = array_fill_keys($dateArray, 'none');
print_r($a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With