【问题标题】:Using PHP DateInterval to create recurring events使用 PHP DateInterval 创建重复事件
【发布时间】:2011-01-12 22:41:49
【问题描述】:

因此,我花了很多时间研究如何最好地将重复事件添加到我的日历应用程序中。

我想使用 PHP 的 DateInterval 函数,并制定了以下代码来尝试解决如何根据原始事件 Start DateFinish DateEndDate 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?

这应该有望创建一个新活动开始日期的列表。 但我还需要为我的周期性活动定义新的结束日期。我该怎么做呢?我需要在事件开始日期 foreach 循环中处理这个吗?

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

【问题讨论】:

    标签: php datetime date


    【解决方案1】:

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

    【讨论】:

    • 什么时候很棒,我现在正在使用它!
    【解决方案2】:

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

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

    【讨论】:

    • 很好......你有没有对这段代码进行过任何改进?检查假期?如果可能,请分享?
    • 嗨@MarcoZen,我们在手动表格中检查hollidays,因为我们尝试的任何api或其他功能在某些特定日期失败。
    • 注明。感谢分享。
    【解决方案3】:

    我使用 strtotime() 来处理这类事情。似乎更容易。像这样工作。

    $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
    

    你甚至可以像这样从任何你想要的日期字符串开始

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

    然后您可以使用 date() 函数以任何格式输出时间戳

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

    如果您有任何问题,请告诉我。

    【讨论】:

    • 您好,感谢您的回答。我已经研究过使用 strtotime() ,但它让我处于与 foreach 循环中的问题相同的位置。我的应用程序要求我生成一批重复事件,我可以为每个重复事件的开始日期而不是结束日期执行这些事件。另外,如何将多天一起批处理以解决 strtotime() 的Repeat every Monday, Wednesday and Friday 问题?
    • strtotime('下周五 + 1 周')?找出开始和结束之间的时间量,并将其添加到新的开始日期。
    • 在某些工作日重复,您必须将它们存储在某个地方,并使用下一个。您可以处理一周的日期,排序,然后将其放在顶部(最早的下一个 X 日期)。
    • 也许将日期转换为朱利安日期会有所帮助。我相信有一些关系,比如被 7 整除,那就是星期天。如果您想要两个日期之间的所有星期一,只需获取具有 mod 7 的儒略日期给 1 并将它们转换回公历日期格式。
    【解决方案4】:

    您可以像这样计算用户定义事件的持续时间的 DateInterval:

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

    然后在您的循环中,只需克隆重复日期并重新添加计算出的$eventDuration

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

    您还应该做一些错误处理,因为您的用户端数据是“2011-01-32”!

    【讨论】:

      【解决方案5】:

      这篇文章很旧,但应该注意的是,在谈论重现时,方法是iCalendar (RFC 5545, RFC 2445) standard

      有几个 PHP 库可以解决这个问题:see in github

      【讨论】:

        猜你喜欢
        • 2015-10-24
        • 2020-11-17
        • 1970-01-01
        • 2012-07-17
        • 2014-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多