3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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; }
based on eOHjK
Output for 7.1.0 - 7.3.0beta1
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
Output for 5.6.0 - 7.0.20
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