Using PHP DateInterval to create recurring events

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

Tags:

Datetime

Php

Date