Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- class daysIterator implements Iterator
- {
- private int $daysOffset = 0;
- private ?DateInterval $dateInterval = null;
- private ?DateTimeImmutable $endDate = null;
- private ?DateTimeImmutable $startDate = null;
- public function current(): mixed
- {
- /*
- * Compute current date as number of days offset from start date.
- * 'invert' indicates whether a negative interval
- */
- $days = new DateInterval(sprintf('P%dD', $this->daysOffset));
- $date = $this->dateInterval->invert == 0 ? $this->startDate->add($days)
- : $this->startDate->sub($days);
- /* reformat current date and return date fields as an integer array */
- $current = explode('/', $date->format('d/m/Y'));
- return ['day' => (int) $current[0],
- 'month' => (int) $current[1],
- 'year' => (int) $current[2],
- ];
- }
- public function key(): mixed
- {
- return $this->daysOffset;
- }
- public function next(): void
- {
- $this->daysOffset++;
- }
- public function rewind(): void
- {
- $this->daysOffset = 0;
- }
- public function valid(): bool
- {
- $this->dateInterval
- or throw new LogicException('Both start and end dates must be specified');
- return $this->daysOffset <= $this->dateInterval->days;
- }
- public function setEndDate(string $endDate)
- {
- $this->endDate = new DateTimeImmutable($endDate);
- $this->dateInterval();
- }
- public function setStartDate(string $startDate)
- {
- $this->startDate = new DateTimeImmutable($startDate);
- $this->dateInterval();
- }
- private function dateInterval()
- {
- if (
- ! is_null($this->startDate)
- && ! is_null($this->endDate)
- ) {
- /* determine interval between dates */
- $this->dateInterval = $this->startDate->diff($this->endDate);
- $this->daysOffset = 0; // holds number of days from start date
- } else {
- $this->dateInterval = null;
- }
- }
- }
- $eol = empty($argc) ? '<br>' : PHP_EOL; // determine end of line
- $days = new daysIterator();
- $days->setStartDate('2023-02-06');
- $days->setEndDate('2023-02-01');
- foreach ($days as $key => $current) {
- var_dump($key, $current);
- echo $eol;
- }
- /**************************************************************************************************/
- class daysIterator_v2 implements Iterator
- {
- private int $daysOffset;
- private DateInterval $dateInterval;
- private DateTimeImmutable $startDate;
- public function __construct(string $startDate, string $endDate)
- {
- $this->startDate = new DateTimeImmutable($startDate);
- /* determine interval between dates */
- $this->dateInterval = $this->startDate->diff(new DateTime($endDate));
- $this->daysOffset = 0; // holds number of days from start date
- }
- public function current(): mixed
- {
- /*
- * Compute current date as number of days offset from start date.
- * 'invert' indicates whether a negative interval
- */
- $days = new DateInterval(sprintf('P%dD', $this->daysOffset));
- $date = $this->dateInterval->invert == 0 ? $this->startDate->add($days)
- : $this->startDate->sub($days);
- /* reformat current date and return date fields as an integer array */
- $current = explode('/', $date->format('d/m/Y'));
- return ['day' => (int) $current[0],
- 'month' => (int) $current[1],
- 'year' => (int) $current[2],
- ];
- }
- public function key(): mixed
- {
- return $this->daysOffset;
- }
- public function next(): void
- {
- $this->daysOffset++;
- }
- public function rewind(): void
- {
- $this->daysOffset = 0;
- }
- public function valid(): bool
- {
- return $this->daysOffset <= $this->dateInterval->days;
- }
- }
- $eol = empty($argc) ? '<br>' : PHP_EOL; // determine end of line
- foreach (new daysIterator_v2('2023-02-06', '2023-02-20') as $key => $current) {
- var_dump($key, $current);
- echo $eol;
- }
Advertisement
Add Comment
Please, Sign In to add comment