<?php
class DateIterator implements \Iterator
{
const FORWARD = 'fwd';
const BACKWARD = 'bwd';
/**
* @var \DateTime
*/
private $from;
/**
* @var \DateTime
*/
private $to;
/**
* @var \DateTime
*/
private $current;
/**
* @var \DateInterval
*/
private $interval;
/**
* @var string
*/
private $direction = self::FORWARD;
public function __construct(\DateTime $from, \DateTime $to, \DateInterval $interval = null)
{
$this->from = $from;
$this->to = $to;
if ($interval === null) {
$interval = new \DateInterval('P1D');
}
$this->interval = $interval;
if ($to < $from) {
$this->direction = self::BACKWARD;
} else {
$this->direction = self::FORWARD;
}
$this->rewind();
}
/**
* Return the current element
*
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
* @since 5.0.0
*/
public function current()
{
return $this->current;
}
/**
* Move forward to next element
*
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function next()
{
if ($this->direction === self::FORWARD) {
$this->current = $this->current->add($this->interval);
} else {
$this->current = $this->current->sub($this->interval);
}
}
/**
* Return the key of the current element
*
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
* @since 5.0.0
*/
public function key()
{
return null;
}
/**
* Checks if current position is valid
*
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
* @since 5.0.0
*/
public function valid()
{
if ($this->direction === self::FORWARD) {
return $this->current < $this->to;
} else {
return $this->current > $this->from;
}
}
/**
* Rewind the Iterator to the first element
*
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function rewind()
{
$this->current = $this->from;
}
}
$from = new \DateTime();
$to = new \DateTime('+1 month');
$i = new DateIterator($from, $to);
foreach ($i as $x) {
echo $x->format('Y-m-d'), PHP_EOL;
}
Deprecated: Return type of DateIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/DlGB0 on line 61
Deprecated: Return type of DateIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/DlGB0 on line 73
Deprecated: Return type of DateIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/DlGB0 on line 89
Deprecated: Return type of DateIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/DlGB0 on line 102
Deprecated: Return type of DateIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/DlGB0 on line 118
2016-08-25
2016-08-26
2016-08-27
2016-08-28
2016-08-29
2016-08-30
2016-08-31
2016-09-01
2016-09-02
2016-09-03
2016-09-04
2016-09-05
2016-09-06
2016-09-07
2016-09-08
2016-09-09
2016-09-10
2016-09-11
2016-09-12
2016-09-13
2016-09-14
2016-09-15
2016-09-16
2016-09-17
2016-09-18
2016-09-19
2016-09-20
2016-09-21
2016-09-22
2016-09-23
2016-09-24
2016-09-25