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','Saturday')) && $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 = '2014-06-27 08:30:00'; $dayStart = '8,30'; $dayEnd = '17,30'; $future = addRollover($currentTime, 65, $dayStart, $dayEnd, true); echo "Results: </br>"; echo $future->format('Y-m-d H:i:s').'</br>';
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 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.7
Results: </br>2014-07-08 10:30:00</br>
Output for 5.2.0 - 5.2.17
Fatal error: Call to undefined method DateTime::add() in /in/eKZsE on line 12
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6
Fatal error: Class 'DateTime' not found in /in/eKZsE on line 7
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_VARIABLE in /in/eKZsE 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/eKZsE on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/eKZsE on line 8
Process exited with code 255.

preferences:
261.85 ms | 401 KiB | 466 Q