2014-04-01 26 views
0

有一天,我有以下功能的笨功能:功能的DatePeriod迭代缺少第二个函数调用

/** 
* Returns Days within a period of time defined by two DateTime objects 
* 
* 
* @param $start DateTime 
* @param $end DateTime 
* @param $points 
* 
* $return['days'][x] 
*/ 
public function getDaysInRange(DateTime $start,DateTime $end){ 
    $this->debug(memory_get_usage()/1024,"Memory Usage in KB"); 
    $this->debug(memory_get_peak_usage()/1024,"Peak Memory Usage in KB"); 
    $this->debug(memory_get_peak_usage(true)/1024,"Real Peak Memory Usage in KB"); 

    $interval = new DateInterval('P1D'); // 1Day Interval 
    $startC= clone $start; 
    $startC->setTime(0,0,0); 
    $endC = clone $end; 
    $endC->setTime(0,0,0); 
    // Workaround because last day of period is not in Iterator 
    $endC->add($interval); 

    $this->debug($startC,"Get Days in Range Start"); 
    $this->debug($endC,"Get Days in Range End"); 

    $daterange = new DatePeriod($startC, $interval,$endC); 

    $return = array('days' => array()); 
    foreach($daterange as $date){ 
     $this->debug($date->format("Y-m-d"),"Single Day in Daterange"); 
     $return['days'][] = $date->format("Y-m-d"); 
    } 

    $this->debug($return,"Return of getDays in Range"); 
    return $return; 
} 

当我调用该函数2个不同的datetime对象倍。

   [start] => DateTime Object 
         (
          [date] => 2014-03-01 00:00:00 
          [timezone_type] => 1 
          [timezone] => +00:00 
         ) 

        [end] => DateTime Object 
         (
          [date] => 2014-03-31 00:00:00 
          [timezone_type] => 1 
          [timezone] => +00:00 
         ) 

     [start] => DateTime Object 
         (
          [date] => 2014-03-01 00:00:00 
          [timezone_type] => 1 
          [timezone] => +00:00 
         ) 

        [end] => DateTime Object 
         (
          [date] => 2014-03-31 00:00:00 
          [timezone_type] => 1 
          [timezone] => +00:00 
         ) 

从时间戳创建:

$begin = DateTime::createFromFormat('U', $filter['start'], new DateTimeZone('UTC')); 
    $end = DateTime::createFromFormat('U', $filter['end'], new DateTimeZone('UTC')); 

的第一次调用返回正确:

(
    [days] => Array 

(
    [0] => 2014-03-01 
    [1] => 2014-03-02 
    [2] => 2014-03-03 
    [3] => 2014-03-04 
    [4] => 2014-03-05 
    [5] => 2014-03-06 
    [6] => 2014-03-07 
    [7] => 2014-03-08 
    [8] => 2014-03-09 
    [9] => 2014-03-10 
    [10] => 2014-03-11 
    [11] => 2014-03-12 
    [12] => 2014-03-13 
    [13] => 2014-03-14 
    [14] => 2014-03-15 
    [15] => 2014-03-16 
    [16] => 2014-03-17 
    [17] => 2014-03-18 
    [18] => 2014-03-19 
    [19] => 2014-03-20 
    [20] => 2014-03-21 
    [21] => 2014-03-22 
    [22] => 2014-03-23 
    [23] => 2014-03-24 
    [24] => 2014-03-25 
    [25] => 2014-03-26 
    [26] => 2014-03-27 
    [27] => 2014-03-28 
    [28] => 2014-03-29 
    [29] => 2014-03-30 
    [30] => 2014-03-31 
) 

) 

第二个呼叫retruns以下,滞后于 “2014年3月25日”

(
    [days] => Array 
     (
      [0] => 2014-03-01 
      [1] => 2014-03-02 
      [2] => 2014-03-03 
      [3] => 2014-03-04 
      [4] => 2014-03-05 
      [5] => 2014-03-06 
      [6] => 2014-03-07 
      [7] => 2014-03-08 
      [8] => 2014-03-09 
      [9] => 2014-03-10 
      [10] => 2014-03-11 
      [11] => 2014-03-12 
      [12] => 2014-03-13 
      [13] => 2014-03-14 
      [14] => 2014-03-15 
      [15] => 2014-03-16 
      [16] => 2014-03-17 
      [17] => 2014-03-18 
      [18] => 2014-03-19 
      [19] => 2014-03-20 
      [20] => 2014-03-21 
      [21] => 2014-03-22 
      [22] => 2014-03-23 
      [23] => 2014-03-24 
      [24] => 2014-03-26 
      [25] => 2014-03-27 
      [26] => 2014-03-28 
      [27] => 2014-03-29 
      [28] => 2014-03-30 
      [29] => 2014-03-31 
     ) 

) 

我首先想到了sid eeffects。我错过了吗?

在php的bugtracker没有具体到我的问题,但一些其他有关DatePeriod的奇怪行为的报告。在服务器上运行一个很老的PHP 5.3.10。

在用较新版本的php重现bug之前,我试图在Codeigniter之外重现它。外面是在各种星座工作。见:http://pastebin.com/FgPTSMEz

所以我想在codeigniter环境中可能会造成内存使用量过大,这可能会导致错误。但codeigniter内部的内存使用量仅为4-5 MB。我在独立的测试文件中复制了5 MB的文本文件。

目前我不知道问题是什么。

+0

'2014-03-25'在你的第一个输出缺失,以及(除非这是一个复制和粘贴错误)。 – CBroe

+1

我敢打赌,这是与DST相关的。时钟在27日在英国发生了变化。 – Jim

+0

@CBroe是c&p错误 – Dukeatcoding

回答

0

发现它的PHP错误#62561固定的5.4

https://bugs.php.net/bug.php?id=62561

在详细的调试原因呼应,您将看到:

Access_Model.php> getDaysInRange(1067) - 单日DATERANGE 2014-03-01 00:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014-03-02 01:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日 2014 -03-03 02:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014-03-04 03:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014 -03-05 04:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014-03-06 05:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014 -03-07 06:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014-03-08 07:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014 -03-09 08:00:00 Access_Model.php> getDaysInRange(1067) - 日期范围中的单日 2014-03-10 09:00:00 Access_Model.php> getDaysInRange(1067) - 单日Daterange 2014-03-11 10:00:00 Access_Model。php> getDaysInRange(1067) - Daterange中的单日 2014-03-12 11:00:00 Access_Model.php> getDaysInRange(1067) - 单日Daterange 2014-03-13 12:00:00 Access_Model。 php> getDaysInRange(1067) - Daterange中的单日期 2014-03-14 13:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014-03-15 14:00:00 Access_Model。 php> getDaysInRange(1067) - Daterange中的单日期 2014-03-16 15:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014-03-17 16:00:00 Access_Model。 php> getDaysInRange(1067) - Daterange中的单日 2014-03-18 17:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014-03-19 18:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014-03-20 19:00:00 Access_Model.php> getDaysInRange(1067) - 日期范围中的单日 2014-03-21 20:00:00 Access_Model.php> getDaysInRange(1067) - 单日Daterange 2014-03-22 21:00:00 Access_Model.php> getDaysInRange(1067) - 日期范围中的单日 2014-03-23 22:00:00 Access_Model.php> getDaysInRange(1067) - 单日Daterange 2014-03-24 23:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日 2014-03-26 00:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014-03-27 01:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日 2014-03-28 02:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日期 2014-03-29 03:00:00 Access_Model.php> getDaysInRange(1067) - Daterange中的单日 二零一四年三月三十零日四点○○分00秒 Access_Model.php> getDaysInRange(1067) - 单日在DATERANGE 2014-03-31 5时00分○○秒

的QuickFix /解决方法:

由于错误是通过与其他时区比当地时区从创建时间戳的日期时间导出我写了下面的quickfix

/** 
* Returns Days within a period of time defined by two DateTime objects 
* 
* 
* @param $start DateTime 
* @param $end DateTime 
* @param $points 
* 
* $return['days'][x] 
*/ 
public function getDaysInRange(DateTime $start,DateTime $end){ 

    // Creation Workaround for Bug https://bugs.php.net/bug.php?id=62561 
    $startC= new DateTime($start->format('Y-m-d'), new DateTimeZone('UTC')) ; 
    $startC->setTime(0,0,0); 
    $endC = new DateTime($end->format('Y-m-d'), new DateTimeZone('UTC')) ; 
    $endC->setTime(0,0,0); 


    $this->debug($startC,"Get Days in Range Start"); 
    $this->debug($endC,"Get Days in Range End"); 

    $return = array('days' => array()); 


    // Workaround because last day of period is not in Iterator 
    $endC->modify('+1 day'); 

while($startC < $endC) { 
      $this->debug($startC->format("Y-m-d H:i:s"),"Single Day in Daterange Modify"); 
    $return['days'][] = $startC->format('Y-m-d'); 
    $startC->modify('+1 day'); 
} 

    $this->debug($return,"Return of getDays in Range"); 
    return $return; 
}