具有DateInterval P1Y的DatePeriod不会在去年生成

时间:2014-01-14 11:08:48

标签: 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->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'] );
    }

}

变量

$this->time = new DateTime(),
self::$data['from'] = 1354316400,
self::$data['to'] = 1389653999

var_dump()输出函数如下:

<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)


执行代码后存储在变量self::$data['zone']中是这样的:

array (size=2)
  0 => 
    array (size=2)
      from => int 1354316400
      to => int 1356994799
  1 => 
    array (size=2)
      from => int 1356994800
      to => int 1389653999


转换为日期:
01.12.2012 - 31.12.2012
01.01.2013 - 13.01.2014
现在,请纠正我,如果它不会产生这个,在这种情况下错误在哪里:
01.12.2012 - 31.12.2012
2013年1月1日 - 2013年12月31日 01.01.2014 - 13.01.2014


其他信息

$ 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

感谢您的回答。

修改

@Glavić:感谢您提供的信息,现在我完全理解了

让我感到震惊的是,如果开始和结束年份相同,并且我在那一年只改变了几个月或几天,它有时会带来正确的结果,有时会缺少去年。

让我进一步解决这个问题。

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

变量

self::$data['from'] = 1354316400 //1.12.2012 00:00:00
self::$data['to'] = 1417388400 //1.12.2014 00:00:00

请注意,self::$data['to']只缺少一秒,差异超过3年。

代码

$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)

在这种情况下,在DatePeriod类中创建了三个日期:
2012-12-01 00:00:00
2013-12-01 00:00:00
2014-12-01 00:00:00
但是删除了最后一个日期,因此类只返回两个DateTime个对象。

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

可变

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)

在这种情况下,在DatePeriod类中创建了四个日期:
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≤x< b}

a =开始日期
b =结束日期
初始化 x 等于 a
然后每个时期的创建 x = x +期间(在我的情况下,P1Y属于{365,366})

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你想要做什么,那么这将对你有所帮助:

$from  = new DateTime('@1354316400');
$to    = new DateTime('@1389653999');
$zones = [];

for ($Y = $from->format('Y'); $Y <= $to->format('Y'); $Y++) {
    $_from = clone $from;
    if ($Y != $from->format('Y')) {
        $_from->setDate($Y, 1, 1);
    }

    $_to = clone $to;
    if ($Y != $to->format('Y')) {
        $_to->setDate($Y, 12, 31);
    }

    $zones[] = [
        'from' => $_from->format('Y-m-d'),
        'to'   => $_to->format('Y-m-d'),
    ];
}

print_r($zones);

demo