0

我将如何构建条件以仅在早上 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;
}
4

1 回答 1

0

我不知道你是否还需要这个,但无论如何它都在这里。需要 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 点之后,或超过 30 分钟的下午 5 点(例如下午 5:30 之后)

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

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

  6. 然后我们进行到第二天

  7. 如果第二天是星期六,我们将进行到第二天

  8. 如果第二天是星期天,我们将前进到第二天

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

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

  11. 我们从函数返回对象

于 2014-01-04T02:57:36.413 回答