3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * Class PeriodIterator * * @author Efimov Evgeniy <evgeniy.efimov@alpari.org> */ class PeriodIterator implements \Iterator { /** * Start date * * @var \DateTime */ private $startDate; /** * End date * * @var \DateTime */ private $endDate; /** * Iteration offset * * @var string */ private $offset; /** * Current iteration date * * @var \DateTime */ private $currentDate; /** * PeriodIterator constructor. * * @param \DateTime $startDate Start date * @param \DateTime $endDate End date * @param string $offset Offset for iteration */ public function __construct(\DateTime $startDate, \DateTime $endDate, $offset) { $this->startDate = $startDate; $this->endDate = $endDate; $this->offset = $offset; } /** {@inheritdoc} */ public function current() { return $this->currentDate; } /** {@inheritdoc} */ public function next() { $this->currentDate->modify($this->offset); } /** {@inheritdoc} */ public function key() { return $this->currentDate->getTimestamp(); } /** {@inheritdoc} */ public function valid() { return $this->currentDate->getTimestamp() <= $this->endDate->getTimestamp(); } /** {@inheritdoc} */ public function rewind() { $this->currentDate = clone $this->startDate; } } $workLoad = new PeriodIterator(new \DateTime('sunday last week'), new DateTime('last day of this month'), "1 day" ); $secondPeriod = $workLoad->rewind($workLoad); echo $secondPeriod->format('Y-m-d H:i:s');
based on YvOku
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught Error: Call to a member function format() on null in /in/slXoT:86 Stack trace: #0 {main} thrown in /in/slXoT on line 86
Process exited with code 255.
Output for 5.6.8 - 5.6.28
Fatal error: Call to a member function format() on null in /in/slXoT on line 86
Process exited with code 255.
Output for 5.3.22 - 5.5.35
Fatal error: Call to a member function format() on a non-object in /in/slXoT on line 86
Process exited with code 255.