Skip to content

Instantly share code, notes, and snippets.

@styks1987
Created May 11, 2017 16:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save styks1987/29dd0f6a68e3b07ba70fec18f732eb86 to your computer and use it in GitHub Desktop.
Save styks1987/29dd0f6a68e3b07ba70fec18f732eb86 to your computer and use it in GitHub Desktop.
<?php
class BusinessDayPeriodIterator implements \Iterator
{
private $current;
private $period = [];
public function __construct(\DatePeriod $period) {
$this->period = $period;
$this->current = $this->period->getStartDate();
if(!$period->include_start_date){
$this->next();
}
$this->endDate = $this->period->getEndDate();
}
public function rewind() {
$this->current->subtract($this->period->getDateInterval());
}
public function current() {
return clone $this->current;
}
public function key() {
return $this->current->diff($this->period->getStartDate());
}
public function next() {
$this->current->add($this->period->getDateInterval());
}
public function valid() {
return $this->current < $this->endDate;
}
public function extend()
{
$this->endDate->add($this->period->getDateInterval());
}
public function isSaturday()
{
return $this->current->format('N') == 6;
}
public function isSunday()
{
return $this->current->format('N') == 7;
}
public function isWeekend()
{
return ($this->isSunday() || $this->isSaturday());
}
}
@rafaelfelipesantos
Copy link

rafaelfelipesantos commented Oct 9, 2020

@manofirmz that is a great idea! I think when I wrote this I was just starting with iterators. BTW, how did you come across this gist?

Here: https://www.php.net/manual/en/class.dateperiod.php

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment