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 DateTime('2010-01-01'); $endDateTime = new DateTime('2010-08-01'); $period = new GgsDatePeriod($startDateTime, 86400, $endDateTime); foreach ($period as $iterDate) { echo $iterDate->format('Y-m-d'); }