我有两个DateTime对象,基本上是To&从价值。我需要遍历此对象并将其输出到表单。由于一些奇怪的原因,它总是一个日期值短。
$this->custom_from_date = new DateTime();
$this->custom_to_date = new DateTime();
//add six days in the future
$this->custom_to_date ->modify("+6 day");
//subtract 1 day from the past
$this->custom_from_date->sub(new DateInterval('P1D'));
//setup date interval between two dates
$interval = new DateInterval('P1D');
$this->setToDate($this->custom_to_date->format('Ymd'));
$this->setFromDate($this->custom_from_date->format('Ymd'));
//create the date range object out of the interval
$daterange = new DatePeriod($this->custom_from_date, $interval, $this->custom_to_date);
然后我尝试迭代日期范围,如下:
foreach ($daterange as $date): ?>
<th>
<?= $date->format('m/d') ?><br> O: R: D:
</th>
<?php endforeach; ?>
输出:
O: R: D: 11/13
O: R: D: 11/14
O: R: D: 11/15
O: R: D: 11/16
O: R: D: 11/17
O: R: D: 11/18
O: R: D: 11/19
它返回一个7索引数组,而不是我预期的8(11/20是最后一个输出)。为什么会这样?
我知道我可以再添加一天来修改方法,但是我需要针对各种sql查询的具体日期。它们都共享相同的自定义日期类,我的结果与我的日期范围对象不匹配。