pep*_*epe 3 php calendar strtotime recurring-events
我目前允许在我的日历应用上进行每日或每周重复活动(使用fullCalendar).
我的视图有一个复选框,可以激活两个下拉菜单:一个用于重复间隔(每天,每周),一个用于频率(一次,两次等).
$evt_interval_options = array( //this will be $row->interval in the model
'86400' => 'Daily',
'604800' => 'Weekly';
$evt_frequency_options = array( //this will be //$i in the model
'1' => '2',
'2' => '3',
<?php echo form_checkbox('evt_repeat', 'FALSE', FALSE, 'class="repeat_checkbox"');
echo form_dropdown('evt_interval', $evt_interval_options');
echo form_dropdown('evt_frequency', $evt_frequency_options'); ?>
Run Code Online (Sandbox Code Playgroud)
这最终到达我的模型,它运行一个循环检查事件是否应重复 - 如果是,它将考虑interval($row->interval)和frequency($i).
$cal_data[] = array(
'start' => strtotime($row->date_1 . ' ' . $row->time_1) + ($i ? ($row->interval * $i) : 0),
);
Run Code Online (Sandbox Code Playgroud)
这很好地基于数据库中的单个记录条目显示多个每日或每周事件.
问题是每月和每年.因为这些将具有可变的秒数,因为
03/01/2011 00:00:00 --> 04/01/2011 00:00:00 ===> 2674800 seconds
04/01/2011 00:00:00 --> 05/01/2011 00:00:00 ===> 2592000 seconds
and so on for monthly and yearly differences
Run Code Online (Sandbox Code Playgroud)
那么我该如何解决这个问题呢?是否有任何功能或strtotime命令可以帮助准确地指出每月应重复的例如4th或每年应重复一次July 4th?
我使用的是PHP 5.2.14.
感谢所有回答 - 我通过@smniep 在strtotime()上的PHP.net手册中解决了这个函数的问题.
function get_x_months_to_the_future( $base_time = null, $months = 1 )
{
if (is_null($base_time))
$base_time = time();
$x_months_to_the_future = strtotime( "+" . $months . " months", $base_time );
$month_before = (int) date( "m", $base_time ) + 12 * (int) date( "Y", $base_time );
$month_after = (int) date( "m", $x_months_to_the_future ) + 12 * (int) date( "Y", $x_months_to_the_future );
if ($month_after > $months + $month_before)
$x_months_to_the_future = strtotime( date("Ym01His", $x_months_to_the_future) . " -1 day" );
return $x_months_to_the_future;
}
Run Code Online (Sandbox Code Playgroud)
这样做是解决天每月定期活动的问题29, 30, 31,以及Feb 28.
我喜欢这个功能,因为它解决了日历应用程序POV中的问题.
如果用户请求从Jan 31正常开始的每月定期事件strtotime将在2月和任何其他没有31天的月份内表现不正常.
正如我在上面的评论中所指出的那样,谷歌日历通过跳过几个没有这样日期的月份来解决这个问题.
我想这是一个偏好问题,但对我来说,提供在一个月的最后一天定期发生的事件更有意义 - 所以在这个Jan 31例子中,重复发生的事件将发生在Feb 28 Mar 31 Apr 30等等.
我将此函数放在一个循环中,该循环迭代用户请求的重复次数并且它完美地工作.它也适用于年度活动,因为我只是通过12了几个月.