【问题标题】:Repeat event on assigned day every week until specific date每周在指定日期重复活动,直到特定日期
【发布时间】:2015-07-09 13:57:34
【问题描述】:

创建事件时,我需要以日期时间格式设置事件开始和事件结束 (0000-00-00 00:00:00) 然后我可以选择每周重复该事件,直到特定日期 (0000-00-00) 但我无法在数据库中正确插入重复事件。这是我所拥有的:

$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); 时,它看起来像这样,没有实际时间只是00:00:00

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

我知道我不能插入这样的值,但我不知道如何从对象中获取正确的值。 所以在这个例子中,我需要插入从 2015 年 4 月 30 日开始到 2015 年 6 月 1 日的每周四的 10:30:00 开始并在 11:30:00 结束的事件(事件总是在同一天结束)。如何实现?

谢谢

【问题讨论】:

    标签: php mysql date datetime


    【解决方案1】:

    问题是您使用日期名称(当前为“星期四”)调用 DateTime::modify。该值是根据$startDateTime 变量计算得出的,然后用于修改该变量,因此它的唯一作用是将DateTime 实例的时间部分重置回0:00:00。

    下面给出了我期望的结果: (我已经注释掉了你需要从你的部分中删除的部分,以便更容易看到不同之处)

    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
    

    【讨论】:

      【解决方案2】:

      当您使用modify() 方法修改$repeatStart 时,您使用的是l 格式字符,根据docs,该格式字符返回

      星期几的完整文本表示

      通过将$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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 2020-08-21
        相关资源
        最近更新 更多