3v4l.org

run code in 300+ PHP versions simultaneously
<?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" ); $workLoad->rewind(); $secondPeriod = $workLoad->current(); echo $firstPer->format('Y-m-d H:i:s');
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Return type of PeriodIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ggrjJ on line 53 Deprecated: Return type of PeriodIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ggrjJ on line 59 Deprecated: Return type of PeriodIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ggrjJ on line 65 Deprecated: Return type of PeriodIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ggrjJ on line 71 Deprecated: Return type of PeriodIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ggrjJ on line 77 Warning: Undefined variable $firstPer in /in/ggrjJ on line 86 Fatal error: Uncaught Error: Call to a member function format() on null in /in/ggrjJ:86 Stack trace: #0 {main} thrown in /in/ggrjJ on line 86
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Deprecated: Return type of PeriodIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ggrjJ on line 53 Deprecated: Return type of PeriodIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ggrjJ on line 59 Deprecated: Return type of PeriodIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ggrjJ on line 65 Deprecated: Return type of PeriodIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ggrjJ on line 71 Deprecated: Return type of PeriodIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ggrjJ on line 77 Warning: Undefined variable $firstPer in /in/ggrjJ on line 86 Fatal error: Uncaught Error: Call to a member function format() on null in /in/ggrjJ:86 Stack trace: #0 {main} thrown in /in/ggrjJ on line 86
Process exited with code 255.
Output for 8.0.0 - 8.0.30
Warning: Undefined variable $firstPer in /in/ggrjJ on line 86 Fatal error: Uncaught Error: Call to a member function format() on null in /in/ggrjJ:86 Stack trace: #0 {main} thrown in /in/ggrjJ on line 86
Process exited with code 255.
Output for 7.0.5 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: firstPer in /in/ggrjJ on line 86 Fatal error: Uncaught Error: Call to a member function format() on null in /in/ggrjJ:86 Stack trace: #0 {main} thrown in /in/ggrjJ on line 86
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught Error: Call to a member function format() on null in /in/ggrjJ:86 Stack trace: #0 {main} thrown in /in/ggrjJ on line 86
Process exited with code 255.
Output for 7.0.0 - 7.0.4
Notice: Undefined variable: firstPer in /in/ggrjJ on line 86 Fatal error: Uncaught Error: Call to a member function format() on unknown in /in/ggrjJ:86 Stack trace: #0 {main} thrown in /in/ggrjJ on line 86
Process exited with code 255.
Output for 5.6.8 - 5.6.28
Notice: Undefined variable: firstPer in /in/ggrjJ on line 86 Fatal error: Call to a member function format() on null in /in/ggrjJ on line 86
Process exited with code 255.
Output for 5.5.24 - 5.5.35
Notice: Undefined variable: firstPer in /in/ggrjJ on line 86 Fatal error: Call to a member function format() on a non-object in /in/ggrjJ on line 86
Process exited with code 255.

preferences:
150.49 ms | 401 KiB | 198 Q