<?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) {
$this->date->modify("+1 day");
if ($this->isBusinessDay($this->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());
Deprecated: Creation of dynamic property BusinessDaysCalculator::$date is deprecated in /in/4IGWN on line 19
Deprecated: Creation of dynamic property BusinessDaysCalculator::$holidays is deprecated in /in/4IGWN on line 20
Deprecated: Creation of dynamic property BusinessDaysCalculator::$nonBusinessDays is deprecated in /in/4IGWN on line 21
Fatal error: Uncaught Error: Call to a member function format() on int in /in/4IGWN:43
Stack trace:
#0 /in/4IGWN(28): BusinessDaysCalculator->isBusinessDay(Object(DateTime))
#1 /in/4IGWN(57): BusinessDaysCalculator->addBusinessDays(3)
#2 {main}
thrown in /in/4IGWN on line 43
Process exited with code 255.
Fatal error: Uncaught Error: Call to a member function format() on int in /in/4IGWN:43
Stack trace:
#0 /in/4IGWN(28): BusinessDaysCalculator->isBusinessDay(Object(DateTime))
#1 /in/4IGWN(57): BusinessDaysCalculator->addBusinessDays(3)
#2 {main}
thrown in /in/4IGWN on line 43
Process exited with code 255.
Fatal error: Uncaught Error: Call to a member function format() on integer in /in/4IGWN:43
Stack trace:
#0 /in/4IGWN(28): BusinessDaysCalculator->isBusinessDay(Object(DateTime))
#1 /in/4IGWN(57): BusinessDaysCalculator->addBusinessDays(3)
#2 {main}
thrown in /in/4IGWN on line 43
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Fatal error: Call to a member function format() on integer in /in/4IGWN on line 43
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38
Fatal error: Call to a member function format() on a non-object in /in/4IGWN on line 43
Process exited with code 255.
Parse error: syntax error, unexpected '[' in /in/4IGWN on line 53
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/4IGWN 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/4IGWN 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/4IGWN 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/4IGWN on line 5
Process exited with code 255.