1

我有这样的代码:

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
01.01.2013 - 31.12 .2013
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})

4

1 回答 1

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

于 2014-01-14T11:48:43.167 回答