Created
May 11, 2017 15:09
-
-
Save styks1987/b15266278cb1ee942a28f4bddc219ab2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PeriodIterator 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()); | |
} | |
} | |
$period = new \DatePeriod($startDateTime, new \DateInterval('P1D'), $endDateTime); | |
$periodIterator = new PeriodIterator($period); | |
$dueDate = clone $startDateTime; | |
while($periodIterator->valid()){ | |
$dueDate = $periodIterator->current(); | |
if(self::isSaturday($dueDate) || self::isSunday($dueDate)){ | |
$periodIterator->extend(); | |
} | |
$periodIterator->next(); | |
} | |
$dueDate->setTime(18, 0, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment