土日を除く、朝8時30分から夜18時30分までの日付にのみ2時間を追加する条件をどのように構成すればよいでしょうか?
国境付近の時間 (たとえば、火曜日の 17:30) が指定されている場合、残りの時間は次の「有効な」時間の開始に追加する必要があります。
たとえば、指定された日付が火曜日の 17:30 の場合、2 時間追加すると、水曜日の 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)。
次のように、単純に 2 時間を追加する方法を知っています。
$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;
}