[#3]
patrick at adrichem dot nu [2014-01-22 11:32:06]
DatePeriod is not compatible with negative intervals.
To do so you can simply use DateInterval and loop through it yourself like this: (not start should be ahead of end if you use a negative interval
class DateRange extends ArrayIterator
{
protected $oDate = null;
protected $oStartDate = null;
protected $oEndDate = null;
protected $oInterval = null;
public function __construct( DateTime $oStartDate, DateTime $oEndDate, DateInterval $oInterval = null )
{
$this->oStartDate = $oStartDate;
$this->oDate = clone $oStartDate;
$this->oEndDate = $oEndDate;
$this->oInterval = $oInterval;
}
public function next()
{
$this->oDate->add($this->oInterval);
return $this->oDate;
}
public function current()
{
return $this->oDate;
}
public function valid()
{
if ($this->oStartDate > $this->oEndDate)
{
return $this->oDate >= $this->oEndDate;
}
return $this->oDate <= $this->oEndDate;
}
}
$oRange = new DateRange(new DateTime("2013-10-01"), new DateTime("2013-01-01"), DateInterval::createFromDateString("-1 month") );
foreach ($oRange as $oDate)
{
echo $oDate->format("Y-m-d") . "
";
}