3v4l.org

run code in 300+ PHP versions simultaneously
<?php function calculate_pickup_time($str_cutoff_time, $int_flags = 0) { if (!preg_match('#^\d{2}:\d{2}$#', $str_cutoff_time)) { throw new \LogicException('Cutoff time must be expressed in 24 hour hh:mm format'); } $obj_now = new DateTime; $int_today = (int)$obj_now->format('N'); $int_add_days = 0; // cutoff time - 2 hr pickup window $obj_pickup_time = DateTime::createFromFormat('H:i', $str_cutoff_time)->sub(new DateInterval('PT2H')); // 2 hr lead time if ($obj_now->diff($obj_pickup_time)->h <= -2) { if (($int_today === 6 && !($int_flags & self::PICKUP_ALLOW_SATURDAY)) || ($int_today === 7 && !($int_flags & self::PICKUP_ALLOW_SUNDAY))) { // we don't deliver today $int_add_days = 8 - $int_today; } } else { if ($int_today === 5 && !($int_flags & self::PICKUP_ALLOW_SATURDAY)) { // today is friday and we don't do saturday $int_add_days = $int_flags & self::PICKUP_ALLOW_SUNDAY ? 2 : 3; } else if ($int_today === 6 && !($int_flags & self::PICKUP_ALLOW_SUNDAY)) { // today is saturday and we don't do sunday $int_add_days = 2; } else { $int_add_days = 1; } } if ($int_add_days > 0) { $obj_pickup_time->add(new DateInterval('P' . $int_add_days . 'D')); } return $obj_pickup_time; } echo calculate_pickup_time('18:00')->format('Y-m-d H:i:s');;
Output for 5.6.21
2016-05-03 16:00:00
Output for 5.6.20
2016-04-11 16:00:00
Output for 5.6.19
2016-03-08 16:00:00
Output for 5.6.18
2016-03-15 16:00:00
Output for 5.6.16
2016-03-28 16:00:00
Output for 5.6.15
2016-03-29 16:00:00
Output for 5.6.14
2016-04-15 16:00:00
Output for 5.6.12
2016-04-21 16:00:00
Output for 5.6.11
2016-04-25 16:00:00
Output for 5.6.10
2016-04-26 16:00:00
Output for 5.6.9
2016-04-29 16:00:00
Output for 5.5.35
2016-05-24 16:00:00
Output for 5.5.34
2016-04-12 16:00:00
Output for 5.5.33
2016-03-09 16:00:00
Output for 5.5.32
2016-03-18 16:00:00
Output for 5.5.31
2016-03-21 16:00:00
Output for 5.5.29
2016-04-18 16:00:00
Output for 5.5.28
2016-04-22 16:00:00
Output for 5.5.26
2016-04-27 16:00:00
Output for 5.5.24
2016-05-25 16:00:00
Output for 5.4.43 - 5.4.45
2015-09-08 16:00:00
Output for 5.4.36 - 5.4.42
2015-06-29 16:00:00
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.35
Fatal error: Cannot access self:: when no class scope is active in /in/i7bQ9 on line 22
Process exited with code 255.
Output for 5.2.10 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/i7bQ9 on line 5 Fatal error: Call to undefined method DateTime::createFromFormat() in /in/i7bQ9 on line 13
Process exited with code 255.
Output for 5.2.0 - 5.2.9
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/i7bQ9 on line 5 Fatal error: Call to undefined method DateTime::createfromformat() in /in/i7bQ9 on line 13
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/i7bQ9 on line 5 Fatal error: Class 'DateTime' not found in /in/i7bQ9 on line 8
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_NEW in /in/i7bQ9 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_NEW in /in/i7bQ9 on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/i7bQ9 on line 5
Process exited with code 255.

preferences:
235.76 ms | 1395 KiB | 155 Q