3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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 7.0.0 - 7.1.0
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 hhvm-3.12.0
Fatal error: Uncaught Error: Class undefined: Date in /in/QtUlS:107 Stack trace: #0 {main}
Process exited with code 255.
Output for hhvm-3.10.0
Fatal error: Class undefined: Date in /in/QtUlS on line 107
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.1 - 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.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.