PHP使用函数在给定的小时内为日期添加两个小时


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+剩余时间。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');

在行动中看到它

以下是对发生的事情的解释:

  1. 首先,我们创建一个DateTime对象,表示我们的开始日期/时间

  2. 然后,我们将指定的时间添加到其中(请参阅支持的日期和时间格式)

  3. 我们检查是周末、下午6点之后还是下午5点超过30分钟(例如下午5:30之后)

  4. 如果是这样,我们克隆我们的datetime对象,并将其设置为下午5:30

  5. 然后,我们将结束时间(下午5:30)和修改后的时间之间的差值作为DateInterval对象

  6. 然后我们进入第二天

  7. 如果第二天是周六,我们将进入第二天

  8. 如果第二天是周日,我们将进入第二天

  9. 然后我们将时间设置为上午8:30

  10. 然后,我们将结束时间(下午5:30)和修改后的时间之间的差异添加到日期时间对象中

  11. 我们从函数返回对象