<?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>';
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.
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.