<?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 >= 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 >= 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 >= 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 >= 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 >= 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, '+1 day', $endDateTime);
foreach ($period as $iterDate) {
echo $iterDate->format('Y-m-d');
}
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/hUWlh 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/hUWlh 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/hUWlh 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/hUWlh 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/hUWlh on line 101
2010-01-012010-01-022010-01-032010-01-042010-01-052010-01-062010-01-072010-01-082010-01-092010-01-102010-01-112010-01-122010-01-132010-01-142010-01-152010-01-162010-01-172010-01-182010-01-192010-01-202010-01-212010-01-222010-01-232010-01-242010-01-252010-01-262010-01-272010-01-282010-01-292010-01-302010-01-312010-02-012010-02-022010-02-032010-02-042010-02-052010-02-062010-02-072010-02-082010-02-092010-02-102010-02-112010-02-122010-02-132010-02-142010-02-152010-02-162010-02-172010-02-182010-02-192010-02-202010-02-212010-02-222010-02-232010-02-242010-02-252010-02-262010-02-272010-02-282010-03-012010-03-022010-03-032010-03-042010-03-052010-03-062010-03-072010-03-082010-03-092010-03-102010-03-112010-03-122010-03-132010-03-142010-03-152010-03-162010-03-172010-03-182010-03-192010-03-202010-03-212010-03-222010-03-232010-03-242010-03-252010-03-262010-03-272010-03-282010-03-292010-03-302010-03-312010-04-012010-04-022010-04-032010-04-042010-04-052010-04-062010-04-072010-04-082010-04-092010-04-102010-04-112010-04-122010-04-132010-04-142010-04-152010-04-162010-04-172010-04-182010-04-192010-04-202010-04-212010-04-222010-04-232010-04-242010-04-252010-04-262010-04-272010-04-282010-04-292010-04-302010-05-012010-05-022010-05-032010-05-042010-05-052010-05-062010-05-072010-05-082010-05-092010-05-102010-05-112010-05-122010-05-132010-05-142010-05-152010-05-162010-05-172010-05-182010-05-192010-05-202010-05-212010-05-222010-05-232010-05-242010-05-252010-05-262010-05-272010-05-282010-05-292010-05-302010-05-312010-06-012010-06-022010-06-032010-06-042010-06-052010-06-062010-06-072010-06-082010-06-092010-06-102010-06-112010-06-122010-06-132010-06-142010-06-152010-06-162010-06-172010-06-182010-06-192010-06-202010-06-212010-06-222010-06-232010-06-242010-06-252010-06-262010-06-272010-06-282010-06-292010-06-302010-07-012010-07-022010-07-032010-07-042010-07-052010-07-062010-07-072010-07-082010-07-092010-07-102010-07-112010-07-122010-07-132010-07-142010-07-152010-07-162010-07-172010-07-182010-07-192010-07-202010-07-212010-07-222010-07-232010-07-242010-07-252010-07-262010-07-272010-07-282010-07-292010-07-302010-07-312010-08-01