Skip to content

Instantly share code, notes, and snippets.

@pocky
Forked from samy/gist:49fc81862c98e30fd503
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pocky/ecdbfb90727a63d811ce to your computer and use it in GitHub Desktop.
Save pocky/ecdbfb90727a63d811ce to your computer and use it in GitHub Desktop.
<?php
/**
* Class Holidays
*/
class Holidays
{
/**
* @var array
*/
protected $days = [];
/**
* @param $day
*/
public function addDay($day)
{
$this->days[] = $day;
}
/**
* @param array $days
*/
public function addDays(array $days)
{
$this->days = array_merge($this->days, $days);
}
/**
* @return array
*/
public function getDays()
{
return $this->days;
}
}
/**
* Class Workingdays
*/
class Workingdays
{
/**
* @var array
*/
protected $days = [];
/**
* @param $day
*/
public function addDay($day)
{
$this->days[] = $day;
}
/**
* @param array $days
*/
public function addDays(array $days)
{
$this->days = array_merge($this->days, $days);
}
/**
* @return array
*/
public function getDays()
{
return $this->days;
}
}
/**
* Class HolidayManager
*/
class HolidayManager
{
/**
* @var DateTime
*/
protected $startDate;
/**
* @var Holidays
*/
protected $holidays;
/**
* @var Workingdays
*/
protected $workingdays;
/**
* @var
*/
protected $endDate;
/**
* @param DateTime $startDate
* @param Holidays $holidays
* @param Workingdays $workingdays
*/
public function __construct(\DateTime $startDate, Holidays $holidays, Workingdays $workingdays)
{
$this->startDate = $startDate;
$this->holidays = $holidays;
$this->workingdays = $workingdays;
}
/**
* @param $delay
* @return DateTime
*/
public function findNextWorkingDay($delay)
{
$day = 1;
while($delay >= $day) {
$currentDate = $this->addDay($this->startDate);
while ($currentDate == $this->startDate) {
$currentDate = $this->addDay($this->startDate->add(new \DateInterval('P1D')));
}
$day++;
}
return $this->startDate;
}
/**
* @param $currentDate
* @return mixed
*/
protected function addDay($currentDate)
{
if (in_array($currentDate->format('Y-m-d'), $this->holidays->getDays())) {
$this->startDate->add(new \DateInterval('P1D'));
}
if (in_array($currentDate->format('*-m-d'), $this->holidays->getDays())) {
$this->startDate->add(new \DateInterval('P1D'));
}
if (!in_array($currentDate->format('N'), $this->workingdays->getDays())) {
return $currentDate;
}
$this->startDate->add(new \DateInterval('P1D'));
}
}
$holidays = new Holidays;
$holidays->addDays(['2015-12-24', '*-12-25']);
$holidays->addDay('*-01-01');
$workingdays = new Workingdays;
$workingdays->addDays(['1', '2']);
$workingdays->addDay('3');
$workingdays->addDay('4');
$workingdays->addDay('5');
$holidayManager = new HolidayManager(new \DateTime('2015-12-01'), $holidays, $workingdays);
$nextBusinessDay = $holidayManager->findNextWorkingDay(45);
var_dump($nextBusinessDay);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment