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) { $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());
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/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.
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27
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.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33
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.
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/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.
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/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.

preferences:
311.76 ms | 401 KiB | 456 Q