为什么我的DatePeriod对象为空?

时间:2015-02-13 19:24:09

标签: php datetime

为什么我的DatePeriod对象为空?时间和间隔有效。我错过了什么?

$datestart    = new DateTime('2015-01-01 00:00:00');
$dateinterval = new DateInterval('P1D');
$dateend      = new DateTime('2015-03-31 23:59:59');
$dateend      = $dateend->modify( '+1 day' ); 
var_dump($datestart);
var_dump($dateinterval);
var_dump($dateend);

$period = new DatePeriod(
    $datestart,
    $dateinterval,
    $dateend
);

var_dump($period);

我的结果:

class DateTime#1 (3) {
  public $date =>
  string(19) "2015-01-01 00:00:00"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(16) "America/New_York"
}
class DateInterval#2 (8) {
  public $y =>
  int(0)
  public $m =>
  int(0)
  public $d =>
  int(1)
  public $h =>
  int(0)
  public $i =>
  int(0)
  public $s =>
  int(0)
  public $invert =>
  int(0)
  public $days =>
  bool(false)
}
class DateTime#3 (3) {
  public $date =>
  string(19) "2015-04-01 23:59:59"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(16) "America/New_York"
}
class DatePeriod#4 (0) {
}

1 个答案:

答案 0 :(得分:2)

迭代显示日期的$ period:

foreach($period as $date) {
    echo $date->format('Y-m-d'), PHP_EOL;
}

您的dateperiod不像预先填充的数组,它会在每次迭代时生成相关数据