![](/img/trans.png)
[英]Add two hours to a date within given hours that rollover to the next timeframe
[英]PHP Add two hours to a date within given hours using function
我如何構造條件,以便僅在早上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;
}
我不知道您是否仍然需要此功能,但是無論如何都可以。 需要PHP 5.3或更高版本
<?php
function addRollover($givenDate, $addtime) {
$datetime = new DateTime($givenDate);
$datetime->modify($addtime);
if (in_array($datetime->format('l'), array('Sunday','Saturday')) ||
17 < $datetime->format('G') ||
(17 === $datetime->format('G') && 30 < $datetime->format('G'))
) {
$endofday = clone $datetime;
$endofday->setTime(17,30);
$interval = $datetime->diff($endofday);
$datetime->add(new DateInterval('P1D'));
if (in_array($datetime->format('l'), array('Saturday', 'Sunday'))) {
$datetime->modify('next Monday');
}
$datetime->setTime(8,30);
$datetime->add($interval);
}
return $datetime;
}
$future = addRollover('2014-01-03 15:15:00', '+4 hours');
echo $future->format('Y-m-d H:i:s');
這是對發生的情況的解釋:
首先,我們創建一個DateTime對象,代表我們的開始日期/時間
然后,我們向其中添加指定的時間量(請參閱支持的日期和時間格式 )
我們檢查是否是周末,下午6點之后,或在下午5點之后經過了30分鍾以上(例如,下午5:30之后)
如果是這樣,我們克隆日期時間對象並將其設置為5:30 PM
然后,我們將結束時間(5:30 PM)與修改后的時間之間的差作為DateInterval對象
然后我們進行到第二天
如果第二天是星期六,我們將進行到第二天
如果第二天是星期日,我們將進行到第二天
然后,我們將時間設置為8:30 AM
然后,我們將結束時間(5:30 PM)與修改后的時間之間的差添加到datetime對象中
我們從函數返回對象
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.