<?php
class BusinessDaysCalculator {
const MONDAY = 1;
const TUESDAY = 2;
const WEDNESDAY = 3;
const THURSDAY = 4;
const FRIDAY = 5;
const SATURDAY = 6;
const SUNDAY = 7;
/**
* DateTime $startDate Date to start calculations from
* DateTime[] $holidays Array of holidays, holidays are no conisdered business days.
* int[] $nonBusinessDays Array of days of the week which are not business days.
*/
public function __construct(DateTime $startDate, array $holidays, array $nonBusinessDays) {
$this->date = $startDate;
$this->holidays = $holidays;
$this->nonBusinessDays = $nonBusinessDays;
}
public function addBusinessDays($howManyDays) {
$i = 0;
while ($i < $howManyDays) {
$date->modify("+1 day");
if ($this->isBusinessDay($date)) {
$i++;
}
}
}
public function getDate() {
return $this->date;
}
private function isBusinessDay(DateTime $date) {
if (in_array((int)$date->format('N'), $this->nonBusinessDays)) {
return false; //Date is a nonBusinessDay.
}
foreach ($this->nonBusinessDays as $day) {
if ($date->format('Y-m-d') == $day->format('Y-m-d')) {
return false; //Date is a holiday.
}
}
return true; //Date is a business day.
}
}
$calculator = new BusinessDaysCalculator(
[new DateTime()], // Today
[new DateTime("2014-06-01"), new DateTime("2014-06-02")],
[BusinessDaysCalculator::SATURDAY, BusinessDaysCalculator::FRIDAY]
);
$calculator->addBusinessDays(3); // Add three business days
var_dump($calculator->getDate());
Fatal error: Uncaught TypeError: BusinessDaysCalculator::__construct(): Argument #1 ($startDate) must be of type DateTime, array given, called in /in/Mjn5W on line 51 and defined in /in/Mjn5W:18
Stack trace:
#0 /in/Mjn5W(51): BusinessDaysCalculator->__construct(Array, Array, Array)
#1 {main}
thrown in /in/Mjn5W on line 18
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27
Fatal error: Uncaught TypeError: BusinessDaysCalculator::__construct(): Argument #1 ($startDate) must be of type DateTime, array given, called in /in/Mjn5W on line 54 and defined in /in/Mjn5W:18
Stack trace:
#0 /in/Mjn5W(54): BusinessDaysCalculator->__construct(Array, Array, Array)
#1 {main}
thrown in /in/Mjn5W on line 18
Process exited with code 255.
Fatal error: Uncaught TypeError: Argument 1 passed to BusinessDaysCalculator::__construct() must be an instance of DateTime, array given, called in /in/Mjn5W on line 54 and defined in /in/Mjn5W:18
Stack trace:
#0 /in/Mjn5W(54): BusinessDaysCalculator->__construct(Array, Array, Array)
#1 {main}
thrown in /in/Mjn5W on line 18
Process exited with code 255.
Catchable fatal error: Argument 1 passed to BusinessDaysCalculator::__construct() must be an instance of DateTime, array given, called in /in/Mjn5W on line 55 and defined in /in/Mjn5W on line 18
Process exited with code 255.
Parse error: syntax error, unexpected '[', expecting ')' in /in/Mjn5W on line 52
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/Mjn5W on line 18
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Mjn5W on line 5
Process exited with code 255.
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Mjn5W on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/Mjn5W on line 5
Process exited with code 255.