3v4l.org

run code in 300+ PHP versions simultaneously
<?php function addRollover($givenDate, $addtime, $dayStart, $dayEnd, $weekDaysOnly) { //Break the working day start and end times into hours, minuets $dayStart = explode(',', $dayStart); $dayEnd = explode(',', $dayEnd); //Create required datetime objects and hours interval $datetime = new DateTime($givenDate); $endofday = clone $datetime; $endofday->setTime($dayEnd[0], $dayEnd[1]); //set end of working day time $interval = 'PT'.$addtime.'H'; //Add hours onto initial given date $datetime->add(new DateInterval($interval)); //if initial date + hours is after the end of working day if($datetime > $endofday) { //get the difference between the initial date + interval and the end of working day in seconds $seconds = $datetime->getTimestamp()- $endofday->getTimestamp(); //Loop to next day while(true) { $endofday->add(new DateInterval('PT24H'));//Loop to next day by adding 24hrs $nextDay = $endofday->setTime($dayStart[0], $dayStart[1]);//Set day to working day start time //If the next day is on a weekend and the week day only param is true continue to add days if(in_array($nextDay->format('l'), array('Sunday')) && $weekDaysOnly) { continue; } else //If not a weekend { $tmpDate = clone $nextDay; $tmpDate->setTime($dayEnd[0], $dayEnd[1]);//clone the next day and set time to working day end time $nextDay->add(new DateInterval('PT'.$seconds.'S')); //add the seconds onto the next day //if the next day time is later than the end of the working day continue loop if($nextDay > $tmpDate) { $seconds = $nextDay->getTimestamp()-$tmpDate->getTimestamp(); $endofday = clone $tmpDate; $endofday->setTime($dayStart[0], $dayStart[1]); } else //else return the new date. { return $endofday; } } } } return $datetime; } $currentTime = '2015-02-08 02:30:00'; $dayStart = '9,00'; $dayEnd = '18,00'; $date = strtotime($currentTime); $currentHour = date('H', $date); if ($currentHour < 9){ echo $date->format('Y-m-d 9:i:s'); } $future = addRollover($currentTime, 10, $dayStart, $dayEnd, true); echo "Results: </br>"; echo $future->format('Y-m-d H:i:s').'</br>';
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.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught Error: Call to a member function format() on int in /in/L4lEX:61 Stack trace: #0 {main} thrown in /in/L4lEX on line 61
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Uncaught Error: Call to a member function format() on int in /in/L4lEX:61 Stack trace: #0 {main} thrown in /in/L4lEX on line 61
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33
Fatal error: Uncaught Error: Call to a member function format() on integer in /in/L4lEX:61 Stack trace: #0 {main} thrown in /in/L4lEX on line 61
Process exited with code 255.
Output for 5.6.0 - 5.6.28
Fatal error: Call to a member function format() on integer in /in/L4lEX on line 61
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 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/L4lEX on line 61
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_VARIABLE in /in/L4lEX on line 8
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_VARIABLE in /in/L4lEX on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/L4lEX on line 8
Process exited with code 255.

preferences:
273.65 ms | 401 KiB | 371 Q