使用PHP DateInterval创建重复事件

Tim*_*Tim 8 php datetime date

所以我花了很多时间研究如何最好地将重复发生的事件添加到我的日历应用程序中.

我想使用PHP DateInterval函数,并制定了下面的代码,试图找出如何根据原始事件创建一个重复事件Start Date,Finish Date以及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?
Run Code Online (Sandbox Code Playgroud)

这应该有希望创建一个新的事件开始日期列表.但我还需要为我的定期事件定义新的结束日期.我该怎么做呢?我是否需要在事件开始日期foreach循环中处理此问题?

我的另一个问题是我如何组合多个dateIntervals来处理Repeat every Monday, Wednesday and Friday?目前只有一个dateIntervals正常工作next friday

谢谢你的帮助!

蒂姆

Bri*_*ian 13

Thomas Planer为PHP 5.2+提供了一个日期/日历递归库.它不是DateInterval,但似乎可以解决问题.请访问https://github.com/tplaner/When查看

我知道这是一个老问题,但也许有人会受益!


ope*_*org 6

嗨,我必须开发类似的东西,我做了流动:

$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);
Run Code Online (Sandbox Code Playgroud)