每周在指定的日期重复事件,直到指定日期为止?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (18)

在创建事件时

$startDateTime = '2015-04-30 10:30:00';
$endDateTime = '2015-04-30 11:30:00';
$repeatEndDate = '2015-06-01';
$timestamp = strtotime($startDateTime);
$day_of_week = date('l', $timestamp);
$step  = 1;
$unit  = 'W';
$repeatStart = new DateTime($startDateTime);
$repeatEnd   = new DateTime($repeatEndDate);
$repeatStart->modify($day_of_week);  
$interval = new DateInterval("P{$step}{$unit}");
$period   = new DatePeriod($repeatStart, $interval, $repeatEnd);

foreach ($period as $key => $date ) {
    $repeatQuery = 'INSERT INTO event(start,end,status,repeats) VALUES ("'.$startDateTime.'","'.$endDateTime.'","'.$status.'","'.$repeatEndDate.'")';
    $repeatResult = mysqli_query($db, $repeatQuery) or die (mysqli_error($db));
}

这么做的时候print_r($date);看起来是这样

DateTime Object
(
    [date] => 2015-04-30 00:00:00
    [timezone_type] => 3
    [timezone] => Europe/Berlin
)

提问于
用户回答回答于

或者尝试下这个:

date_default_timezone_set('Etc/UTC');

$startDateTime = '2015-04-30 10:30:00';
$endDateTime = '2015-04-30 11:30:00';
$repeatEndDate = '2015-06-01';
#$timestamp = strtotime($startDateTime);
#$day_of_week = date('l', $timestamp);
$step  = 1;
$unit  = 'W';
$repeatStart = new DateTime($startDateTime);
$repeatEnd   = new DateTime($repeatEndDate);
#$repeatStart->modify($day_of_week);  
$interval = new DateInterval("P{$step}{$unit}");
$period   = new DatePeriod($repeatStart, $interval, $repeatEnd);



foreach ($period as $key => $date ) {
    echo($date->format('Y-m-d H:i:s')) . PHP_EOL;
}

运行上面的结果是:

2015-04-30 10:30:00
2015-05-07 10:30:00
2015-05-14 10:30:00
2015-05-21 10:30:00
2015-05-28 10:30:00
用户回答回答于

通过更改$day_of_week

$day_of_week = date('Y-m-d H:i:s', $timestamp);

到以下输出

DateTime Object
(
   [date] => 2015-04-30 10:30:00
   [timezone_type] => 3
   [timezone] => Europe/London
)
DateTime Object
(
    [date] => 2015-05-07 10:30:00
    [timezone_type] => 3
    [timezone] => Europe/London
)
DateTime Object
(
    [date] => 2015-05-14 10:30:00
    [timezone_type] => 3
    [timezone] => Europe/London
) 
DateTime Object
(
    [date] => 2015-05-21 10:30:00
    [timezone_type] => 3
    [timezone] => Europe/London
)
DateTime Object
(
    [date] => 2015-05-28 10:30:00
    [timezone_type] => 3
    [timezone] => Europe/London
)

不过,修改实际上并不必要,下面的代码应该可以实现您所要寻找的内容。

 <?php

$startDateTime = '2015-04-30 10:30:00';
$endDateTime = '2015-04-30 11:30:00';
$repeatEndDate = '2015-06-01';

$step  = 1;
$unit  = 'W';
$repeatStart = new DateTime($startDateTime);

$repeatEnd   = new DateTime($repeatEndDate);

$interval = new DateInterval("P{$step}{$unit}");
$period   = new DatePeriod($repeatStart, $interval, $repeatEnd);
foreach ($period as $key => $date ) {
    $repeatQuery = 'INSERT INTO event(start,end,status,repeats) VALUES ("'.$startDateTime.'","'.$endDateTime.'","'.$status.'","'.$repeatEndDate.'")';
    $repeatResult = mysqli_query($db, $repeatQuery) or die (mysqli_error($db));
    print_r($date);
}

扫码关注云+社区