Php DatePeriod与DateInterval P1Y去年未生成

Php DatePeriod与DateInterval P1Y去年未生成,php,datetime,dateinterval,Php,Datetime,Dateinterval,我有这样一个密码: private function validatePeriodArrayYear() { $this->time->setTimestamp( self::$data['to'] ); $year1 = $this->time->format('Y'); $this->time->setTimestamp( self::$data['from'] ); $year2 = $this->time->

我有这样一个密码:

private function validatePeriodArrayYear()
{
    $this->time->setTimestamp( self::$data['to'] );
    $year1 = $this->time->format('Y');
    $this->time->setTimestamp( self::$data['from'] );
    $year2 = $this->time->format('Y');

    $overlap = $year1 - $year2;

    if( $overlap > 0 ){
        $end = clone $this->time;
        $end->setTimestamp( self::$data['to'] );
        $overlap++;

        $interval = new DateInterval('P1Y');
        $period = new DatePeriod( $this->time, $interval, $end );
        var_dump($period);
        $iteration = -1;

        foreach( $period as $onePeriod ){ 
            var_dump($onePeriod);
            $iteration++;
            if( $iteration > 0 ){
                $onePeriod->setDate( $onePeriod->format('Y'), 1, 1 );
            }
            $from = $onePeriod->getTimestamp();
            $onePeriod->setDate( $onePeriod->format('Y'), 12, 31 );
            $onePeriod->setTime( 23, 59, 59 );
            self::$data['zone'][$iteration] = array( 'from' => $from, 'to' => $onePeriod->getTimestamp()  );
        }

        self::$data['zone'][$iteration]['to'] = self::$data['to'];

    } else {
        self::$data['zone'][] = array( 'from' => self::$data['from'], 'to' => self::$data['to'] );
    }

}
<pre>
    object(DatePeriod)[337]
  public start => 
    object(DateTime)[336]
      public date => string 2012-12-01 00:00:00 (length=19)
      public timezone_type => int 3
      public timezone => string Europe/Prague (length=13)
  public current => null
  public end => 
    object(DateTime)[48]
      public date => string 2014-01-13 23:59:59 (length=19)
      public timezone_type => int 3
      public timezone => string Europe/Prague (length=13)
  public interval => 
    object(DateInterval)[331]
      public y => int 1
      public m => int 0
      public d => int 0
      public h => int 0
      public i => int 0
      public s => int 0
      public weekday => int 0
      public weekday_behavior => int 0
      public first_last_day_of => int 0
      public invert => int 0
      public days => boolean false
      public special_type => int 0
      public special_amount => int 0
      public have_weekday_relative => int 0
      public have_special_relative => int 0
  public recurrences => int 1
  public include_start_date => boolean true



object(DateTime)[326]
  public date => string 2012-12-01 00:00:00 (length=19)
  public timezone_type => int 3
  public timezone => string Europe/Prague (length=13)



object(DateTime)[47]
  public date => string 2013-12-01 00:00:00 (length=19)
  public timezone_type => int 3
  public timezone => string Europe/Prague (length=13)
变量 var_dump()输出函数如下所示:


转换为日期:
2012年12月1日至2012年12月31日
2013年1月1日至2014年1月13日
现在,如果它不会生成此错误,请更正我,本例中的错误在哪里:
2012年12月1日至2012年12月31日
2013年1月1日-2013年12月31日
2014年1月1日至2014年1月13日

补充资料 谢谢你的回答

编辑 @格拉维奇:谢谢你提供的信息,现在我完全明白了

我突然想到,如果开始和结束的年份是一样的,而我在那一年中只改变了几个月或几天,有时会带来正确的结果,有时会缺少去年的结果

因此,让我进一步分析一下这个问题。

为了澄清,我将使用不同的输入变量,以便更好地理解

变量 请注意,
self::$data['to']
只是缺少了一秒钟,差异超过了3年

代码 输出 在这种情况下,将在
DatePeriod
class:
内创建三个日期 2012-12-01 00:00:00
2013-12-01 00:00:00
2014-12-01 00:00:00
但最后一个日期被删除,因此类只返回两个
DateTime
对象

现在,我们向变量
self::$data['to']
添加第二个变量,然后再次运行代码

变量 输出 在这种情况下,在
DatePeriod
class:
内创建四个日期 2012-12-01 00:00:00
2013-12-01 00:00:00
2014-12-01 00:00:00
2014-12-01 00:00:01
最后日期再次被删除

数学表达 {x|a≤ xa=开始日期
b=结束日期
初始化的x等于a
然后创建每个时段

x=x+句点(在我的例子中,P1Y属于{365366})

如果我正确理解了您试图做的事情,那么这将帮助您:

$from=newdatetime(“@1354316400”);
$to=新日期时间(“@138965399”);
$zones=[];
对于($Y=$from->format('Y');$Y format('Y');$Y++){
$\u from=clone$from;
如果($Y!=$from->format('Y')){
$\u from->setDate($Y,1,1);
}
$\u to=克隆$to;
如果($Y!=$to->格式化('Y')){
$\u to->setDate($Y,12,31);
}
$zones[]=[
'from'=>$\u from->format('Y-m-d'),
'至'=>$至->格式('Y-m-d'),
];
}
打印区($zones);

是的,重写方法是解决方案,但我想知道为什么
DatePeriod
没有为年份创建
DateTime
的最后一个对象2014@user3193454:endDate不包括在
DatePeriod
中。您可以通过在将endDate发送到DatePeriod之前增加DateInterval来解决这个问题。
<pre>
    object(DatePeriod)[337]
  public start => 
    object(DateTime)[336]
      public date => string 2012-12-01 00:00:00 (length=19)
      public timezone_type => int 3
      public timezone => string Europe/Prague (length=13)
  public current => null
  public end => 
    object(DateTime)[48]
      public date => string 2014-01-13 23:59:59 (length=19)
      public timezone_type => int 3
      public timezone => string Europe/Prague (length=13)
  public interval => 
    object(DateInterval)[331]
      public y => int 1
      public m => int 0
      public d => int 0
      public h => int 0
      public i => int 0
      public s => int 0
      public weekday => int 0
      public weekday_behavior => int 0
      public first_last_day_of => int 0
      public invert => int 0
      public days => boolean false
      public special_type => int 0
      public special_amount => int 0
      public have_weekday_relative => int 0
      public have_special_relative => int 0
  public recurrences => int 1
  public include_start_date => boolean true



object(DateTime)[326]
  public date => string 2012-12-01 00:00:00 (length=19)
  public timezone_type => int 3
  public timezone => string Europe/Prague (length=13)



object(DateTime)[47]
  public date => string 2013-12-01 00:00:00 (length=19)
  public timezone_type => int 3
  public timezone => string Europe/Prague (length=13)
array (size=2)
  0 => 
    array (size=2)
      from => int 1354316400
      to => int 1356994799
  1 => 
    array (size=2)
      from => int 1356994800
      to => int 1389653999
$ php -v
PHP 5.5.6-1+debphp.org~raring+2 (cli) (built: Nov 21 2013 14:39:09) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
    with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 13.10
Release:    13.10
Codename:   saucy

$ uname -a
Linux userName-K55VJ 3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
self::$data['from'] = 1354316400 //1.12.2012 00:00:00
self::$data['to'] = 1417388400 //1.12.2014 00:00:00
$start = new DateTime()->setTimestamp( self::$data['from'] );
$end = new DateTime()->setTimestamp( self::$data['to'] );
$interval = new DateInterval('P1Y');
$period = new DatePeriod( $start, $interval, $end );
foreach( $period as $onePeriod ){
    var_dump($onePeriod);
}
object(DateTime)
  public date => string 2012-12-01 00:00:00 (length=19)
  public timezone_type => int 3
  public timezone => string Europe/Prague (length=13)

object(DateTime)
  public date => string 2013-12-01 00:00:00 (length=19)
  public timezone_type => int 3
  public timezone => string Europe/Prague (length=13)
self::$data['from'] = 1354316400 //1.12.2012 00:00:00
self::$data['to'] = 1417388401 //1.12.2014 00:00:01
object(DateTime)
  public date => string 2012-12-01 00:00:00 (length=19)
  public timezone_type => int 3
  public timezone => string Europe/Prague (length=13)

object(DateTime)
  public date => string 2013-12-01 00:00:00 (length=19)
  public timezone_type => int 3
  public timezone => string Europe/Prague (length=13)

object(DateTime)
  public date => string 2014-12-01 00:00:00 (length=19)
  public timezone_type => int 3
  public timezone => string Europe/Prague (length=13)