【问题标题】:PHP Add two hours to a date within given hours using functionPHP使用函数在给定时间内将两个小时添加到日期
【发布时间】:2013-11-26 22:14:13
【问题描述】:

我将如何构造条件以仅在早上 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 date datetime date-math


    【解决方案1】:

    我不知道你是否还需要这个,但无论如何都在这里。 需要 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');
    

    See it in action

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

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

    2. 然后我们添加指定的时间量(见Supported Date and Time Formats

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

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

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

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

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

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

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

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

    11. 我们从函数中返回对象

    【讨论】:

    • 谢谢,这是一个很棒的功能,但它会在星期天返回。
    • 我添加了 $future = addRollover('2014-06-26 11:41:00', '+63 hours'); echo $future->format('Y-m-d H:i:s');它返回 2014-06-29 17:41:00 这是一个星期天。
    • 我以为你忘记了这个问题!我确认了您的测试并且修复很简单。我只需要将$endofday-&gt;diff($datetime); 更改为$datetime-&gt;diff($endofday);。之前的结果是否定的,减去了我们尝试添加的时间。
    • 我不是问题的原始发帖人,但我需要一些非常相似的东西,在这里查看我的帖子:stackoverflow.com/questions/24427553/…
    • 如果我把这个输入放在 :2014-06-26 15:17:00', '+36 hours' 它仍然不正确它返回 2014-06-30 22:43:00但 22:43 不在 08:30 至 17:30 的工作时间。 Surly 它应该再次滚动到第二天吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多