PHP Добавить два часа к дате в течение заданных часов, используя функцию
Как бы я структурировал условия, чтобы добавить два часа только к датам с 08:30 утра до 18:30 вечера, исключая субботу и воскресенье?
В случае, если указано время около границы (например, 17:30 во вторник), оставшееся время должно быть добавлено к началу следующего «действительного» периода времени.
Например: если указанная дата была во вторник в 17:30, добавление в течение двух часов привело бы к 9:30 в среду (17:30 + 1 час = 18:30, 8:30 + остаток 1 час = 9: 30). Или, если указанная дата была в 17:00 в пятницу, результат будет 9:00 в понедельник (17:00 пятница + 1,5 часа = 18:30, 8:30 понедельник + оставшиеся 0,5 часа = 9:00)
Я знаю, как просто добавить два часа, следующим образом:
$idate1 = strtotime($_POST['date']);
$time1 = date('Y-m-d G:i', strtotime('+120 minutes', $idate1));
$_POST['due_date'] = $time1;
Я пробовал эту функцию, и она прекрасно работает, за исключением случаев, когда я использую дату, как (2013-11-26 12:30), он дает мне (2013-11-27 04:30:00) проблема с 12:30
function addRollover($givenDate, $addtime) {
$starttime = 8.5*60; //Start time in minutes (decimal hours * 60)
$endtime = 18.5*60; //End time in minutes (decimal hours * 60)
$givenDate = strtotime($givenDate);
//Get just the day portion of the given time
$givenDay = strtotime('today', $givenDate);
//Calculate what the end of today's period is
$maxToday = strtotime("+$endtime minutes", $givenDay);
//Calculate the start of the next period
$nextPeriod = strtotime("tomorrow", $givenDay); //Set it to the next day
$nextPeriod = strtotime("+$starttime minutes", $nextPeriod); //And add the starting time
//If it's the weekend, bump it to Monday
if(date("D", $nextPeriod) == "Sat") {
$nextPeriod = strtotime("+2 days", $nextPeriod);
}
//Add the time period to the new day
$newDate = strtotime("+$addtime", $givenDate);
//print "$givenDate -> $newDate\n";
//print "$maxToday\n";
//Get the new hour as a decimal (adding minutes/60)
$hourfrac = date('H',$newDate) + date('i',$newDate)/60;
//print "$hourfrac\n";
//Check if we're outside the range needed
if($hourfrac < $starttime || $hourfrac > $endtime) {
//We're outside the range, find the remainder and add it on
$remainder = $newDate - $maxToday;
//print "$remainder\n";
$newDate = $nextPeriod + $remainder;
}
return $newDate;
}