3v4l.org

run code in 300+ PHP versions simultaneously
<?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->format('Y-m-d'); } /** * 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; }
Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
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/eOHjK 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/eOHjK 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/eOHjK 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/eOHjK 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/eOHjK on line 118 Fatal error: Uncaught Error: Call to a member function format() on string in /in/eOHjK:130 Stack trace: #0 {main} thrown in /in/eOHjK on line 130
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Fatal error: Uncaught Error: Call to a member function format() on string in /in/eOHjK:130 Stack trace: #0 {main} thrown in /in/eOHjK on line 130
Process exited with code 255.
Output for 5.6.0 - 5.6.25
Fatal error: Call to a member function format() on string in /in/eOHjK on line 130
Process exited with code 255.

preferences:
193.19 ms | 401 KiB | 196 Q