3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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());
Output for 7.0.0 - 7.1.0
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.
Output for hhvm-3.12.0
Catchable fatal error: Argument 1 passed to BusinessDaysCalculator::__construct() must be an instance of DateTime, array given in /in/Mjn5W on line 22
Process exited with code 255.
Output for hhvm-3.10.0

Process exited with code 153.
Output for 5.4.0 - 5.6.28
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.
Output for 5.1.0 - 5.3.29
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.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
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.