note 114188 added to class.dateperiod
| From: | patrick at adrichem dot nu | Date: | Wed, 22 Jan 2014 11:32:08 +0000 |
| Subject: | note 114188 added to class.dateperiod | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198301@lists.php.net to get a copy of this message | ||
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") . "<br />";
}
----
Server IP: 91.199.167.223
Probable Submitter: 82.148.208.233
----
Manual Page -- http://php.net/manual/en/class.dateperiod.php
Edit -- https://master.php.net/note/edit/114188
Del: integrated -- https://master.php.net/note/delete/114188/integrated
Del: useless -- https://master.php.net/note/delete/114188/useless
Del: bad code -- https://master.php.net/note/delete/114188/bad+code
Del: spam -- https://master.php.net/note/delete/114188/spam
Del: non-english -- https://master.php.net/note/delete/114188/non-english
Del: in docs -- https://master.php.net/note/delete/114188/in+docs
Del: other reasons-- https://master.php.net/note/delete/114188
Reject -- https://master.php.net/note/reject/114188
Search -- https://master.php.net/manage/user-notes.php