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

PHP使用函数在给定的小时内为日期添加两个小时,php,date,datetime,date-math,Php,Date,Datetime,Date Math,我将如何构造条件,以便在上午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)

我将如何构造条件,以便在上午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;
}
函数addRollover($givenDate,$addtime){
$starttime=8.5*60;//以分钟为单位的开始时间(十进制小时*60)
$endtime=18.5*60;//以分钟为单位的结束时间(十进制小时*60)
$givenDate=strotime($givenDate);
//只获取给定时间的一天部分
$givenDay=strotime('today',$givenDate);
//计算今天的期末是多少
$maxToday=strottime(“+$endtime分钟“,$givenDay);
//计算下一阶段的开始时间
$nextPeriod=strotime(“明天”;$givenDay);//设置为第二天
$nextPeriod=strotime(“+$starttime minutes”,$nextPeriod);//并添加开始时间
//如果是周末,就推迟到星期一
如果(日期(“D”,下一个周期)=“Sat”){
$nextPeriod=strottime(“+2天,$nextPeriod”);
}
//将时间段添加到新的日期
$newDate=strottime(“+$addtime”,$givenDate);
//打印“$givenDate->$newDate\n”;
//打印“$maxToday\n”;
//以十进制形式获取新的小时数(添加分钟/60)
$hourfrac=日期('H',$newDate)+日期('i',$newDate)/60;
//打印“$hourfrac\n”;
//检查我们是否超出所需的范围
如果($hourfrac<$starttime | |$hourfrac>$endtime){
//我们在范围之外,找到余数并添加它
$rements=$newDate-$maxToday;
//打印“$rements\n”;
$newDate=$nextPeriod+$resident;
}
返回$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');

谢谢这是一个很棒的功能,但它将在周日返回。我添加了$future=addRollover('2014-06-26 11:41:00','+63小时');echo$future->format('Y-m-dh:i:s');它返回了2014-06-29 17:41:00,这是一个星期天。我还以为你忘了这个问题呢!我确认了你的测试,修复很容易。我只需要更改
$endofday->diff($datetime)
$datetime->diff($endofday)。之前的结果是否定的,减去了我们试图添加的时间。我不是问题的原始海报,但我需要一些非常类似的东西,请查看我在这里的帖子:如果我将此输入输入:2014-06-26 15:17:00',它仍然不正确,“+36小时”返回2014-06-30 22:43:00,但22:43超出了08:30至17:30的工作时间。粗暴地说,它应该再次滚动到第二天?