3v4l.org

run code in 300+ PHP versions simultaneously
<?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 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Deprecated: Creation of dynamic property BusinessDaysCalculator::$date is deprecated in /in/NR1Cf on line 19 Deprecated: Creation of dynamic property BusinessDaysCalculator::$holidays is deprecated in /in/NR1Cf on line 20 Deprecated: Creation of dynamic property BusinessDaysCalculator::$nonBusinessDays is deprecated in /in/NR1Cf on line 21 Warning: Undefined variable $date in /in/NR1Cf on line 27 Fatal error: Uncaught Error: Call to a member function modify() on null in /in/NR1Cf:27 Stack trace: #0 /in/NR1Cf(57): BusinessDaysCalculator->addBusinessDays(3) #1 {main} thrown in /in/NR1Cf on line 27
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27
Warning: Undefined variable $date in /in/NR1Cf on line 27 Fatal error: Uncaught Error: Call to a member function modify() on null in /in/NR1Cf:27 Stack trace: #0 /in/NR1Cf(57): BusinessDaysCalculator->addBusinessDays(3) #1 {main} thrown in /in/NR1Cf on line 27
Process exited with code 255.
Output for 7.0.5 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: date in /in/NR1Cf on line 27 Fatal error: Uncaught Error: Call to a member function modify() on null in /in/NR1Cf:27 Stack trace: #0 /in/NR1Cf(57): BusinessDaysCalculator->addBusinessDays(3) #1 {main} thrown in /in/NR1Cf on line 27
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught Error: Call to a member function modify() on null in /in/NR1Cf:27 Stack trace: #0 /in/NR1Cf(57): BusinessDaysCalculator->addBusinessDays(3) #1 {main} thrown in /in/NR1Cf on line 27
Process exited with code 255.
Output for 7.0.0 - 7.0.4
Notice: Undefined variable: date in /in/NR1Cf on line 27 Fatal error: Uncaught Error: Call to a member function modify() on unknown in /in/NR1Cf:27 Stack trace: #0 /in/NR1Cf(57): BusinessDaysCalculator->addBusinessDays(3) #1 {main} thrown in /in/NR1Cf on line 27
Process exited with code 255.
Output for 5.6.0 - 5.6.28
Notice: Undefined variable: date in /in/NR1Cf on line 27 Fatal error: Call to a member function modify() on null in /in/NR1Cf on line 27
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38
Notice: Undefined variable: date in /in/NR1Cf on line 27 Fatal error: Call to a member function modify() on a non-object in /in/NR1Cf on line 27
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/NR1Cf 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/NR1Cf 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/NR1Cf on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/NR1Cf 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/NR1Cf on line 5
Process exited with code 255.

preferences:
267.8 ms | 401 KiB | 354 Q