11

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

5 Answers 5

15

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.

Sign up to request clarification or add additional context in comments.

1 Comment

When is awesome, and I'm now using it!
9

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);

2 Comments

Nice ... Did u ever make any improvments to this code ? Check against holidays ? Pls do share if possible ?
Hi @MarcoZen we check hollidays in a manual table as any api or other functions that we try fail with some specific days.
0

i use strtotime() for this sort of thing. seems to be easier. works like this.

$new_timestamp = strtotime('+1 week', time()); // + 1 week from today
$new_timestamp = strtotime('+1 week', $new_timestamp); // + 2 weeks from now
$new_timestamp = strtotime('+1 month', $new_timestamp); // + 1 month and 2 weeks from now

you can even start with whatever date string you want like this

$new_timestamp = strtotime('+1 week', strtotime('2011-01-31 09:00:00')); // one week from 2011-01-31

then you can output the timestamp in any format using the date() function

echo date('Y-m-d', $new_timestamp);

Let me know if you have questions.

4 Comments

Hello, Thanks for your answer. I have looked into using strtotime() but it leaves me in the same position with the problem in the foreach loop. My application requires me to generate a batch of recurring events which I can do for the start date of each recurring event but not the end date. Also, how can I batch multiple days together to solve the Repeat every Monday, Wednesday and Friday issue with strtotime()?
strtotime('next friday + 1 week')? figure out the amount of time between the start and end, and add it to your new start date.
repeat on certain weekdays, youd have to keep them stored somewhere, and use whatever the next one is. you could process a weeks worth of dates, sort, then put the one at the top (earliest next X date).
maybe converting dates to julian dates will help. i believe there is some relation like being evenly divisible by 7 means that is a sunday. if you want all mondays between two dates, just get the julian dates which have mod 7 give 1 and convert them back to gregorian date format.
0

You could calculate a DateInterval for the duration of the user defined events like so:

$eventStart = new DateTime( '2011-01-31 09:00:00' );
$eventFinish = new DateTime( '2011-01-31 17:00:00' );
$eventDuration = $eventStart->diff($eventFinish);

then in your loop, just clone the recurring date and add the calculated $eventDuration back on

$until = clone $dt;
$until->add($eventDuration);

Also you should do some error handling as your user end data is "2011-01-32"!

Comments

0

This post is old but it should be noted that when talking about recurrence the way is the iCalendar (RFC 5545, RFC 2445) standard

there are several PHP libraries that address this issue: see in github

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.