3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
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.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.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.