3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Workaround for date period iteration under php 5.2 * * @author pgorbach <pgorbach@goodgamestudios.com> * @package publishers * @version $Id$ */ class GgsDatePeriod implements Iterator { /** * @var \DateTime */ protected $current; /** * @var \DateTime */ protected $start; /** * @var \DateTime */ protected $end; /** * @var string */ protected $interval; /** * @todo replace this class by build in php class DatePeriod * * @param \DateTime $start * @param string $interval * @param \DateTime $end * @param int $options */ public function __construct($start, $interval, $end, $options = 0) { $this->start = $start; $this->end = $end; $this->interval = $interval; $this->current = clone($start); } /** * (PHP 5 &gt;= 5.0.0)<br/> * Return the current element * * @link http://php.net/manual/en/iterator.current.php * @return mixed Can return any type. */ public function current() { return $this->current; } /** * (PHP 5 &gt;= 5.0.0)<br/> * Move forward to next element * * @link http://php.net/manual/en/iterator.next.php * @return void Any returned value is ignored. */ public function next() { $this->current->modify($this->interval); } /** * (PHP 5 &gt;= 5.0.0)<br/> * 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. */ public function key() { return strtotime($this->current()->format('Y-m-d')); } /** * (PHP 5 &gt;= 5.0.0)<br/> * 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. */ public function valid() { return $this->current() <= $this->end; } /** * (PHP 5 &gt;= 5.0.0)<br/> * Rewind the Iterator to the first element * * @link http://php.net/manual/en/iterator.rewind.php * @return void Any returned value is ignored. */ public function rewind() { $this->current = clone($this->start); } } $startDateTime = new Date('2010-01-01'); $endDateTime = new Date('2010-08-01'); $period = new GgsDatePeriod($startDateTime, 86400, $endDateTime); foreach ($period as $iterDate) { echo $iterDate->format('Y-m-d'); }
Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Deprecated: Return type of GgsDatePeriod::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/QtUlS on line 52 Deprecated: Return type of GgsDatePeriod::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/QtUlS on line 64 Deprecated: Return type of GgsDatePeriod::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/QtUlS on line 76 Deprecated: Return type of GgsDatePeriod::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/QtUlS on line 89 Deprecated: Return type of GgsDatePeriod::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/QtUlS on line 101 Fatal error: Uncaught Error: Class "Date" not found in /in/QtUlS:107 Stack trace: #0 {main} thrown in /in/QtUlS on line 107
Process exited with code 255.
Output for 8.0.0 - 8.0.30
Fatal error: Uncaught Error: Class "Date" not found in /in/QtUlS:107 Stack trace: #0 {main} thrown in /in/QtUlS on line 107
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Class 'Date' not found in /in/QtUlS:107 Stack trace: #0 {main} thrown in /in/QtUlS on line 107
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.1 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
Fatal error: Class 'Date' not found in /in/QtUlS on line 107
Process exited with code 255.
Output for 5.1.0
Fatal error: Trying to clone an uncloneable object of class date in /in/QtUlS on line 42
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/QtUlS on line 10
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/QtUlS on line 10
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/QtUlS on line 10
Process exited with code 255.

preferences:
260.51 ms | 401 KiB | 350 Q