将两个小时添加到指定小时内的日期,该日期会滚动到下一个时间范围 [英] Add two hours to a date within given hours that rollover to the next timeframe

查看:40
本文介绍了将两个小时添加到指定小时内的日期,该日期会滚动到下一个时间范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何构建条件以仅在早上 08:30 到晚上 18:30 之间的日期添加两个小时,不包括星期六和星期日?

How would I structure the conditions to add two hours only to dates between 08:30 in the morning until 18:30 of the evening, excluding Saturday and Sunday?

如果给出了靠近边界的时间(例如周二的 17:30),则应将剩余的时间添加到下一个有效"时间段的开头.

In the case that a time near the border (e.g. 17:30 on Tuesday) is given, the left over time should be added to the beginning of the next "valid" time period.

例如:如果给定的日期是星期二的 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)

For example: if the given date was in 17:30 on Tuesday, the two hour addition would result in 9:30 on Wednesday (17:30 + 1 hour = 18:30, 8:30 + the remainder 1 hour = 9:30). Or if the given date was in 17:00 on Friday, the result would be 9:00 on Monday (17:00 Friday + 1.5 hours = 18:30, 8:30 Monday + the remainder .5 hours = 9:00)

我知道如何简单地添加两个小时,如下所示:

I know how to simply add two hours, as follows:

$idate1 = strtotime($_POST['date']);
$time1 = date('Y-m-d G:i', strtotime('+120 minutes', $idate1));
$_POST['due_date']  = $time1;

但是我要添加什么来完成上述任务?

But what would I add to accomplish the above?

推荐答案

使用幻数"和对日期进行大量数学运算会使您的代码可读性降低,并且还可能导致问题 - 它不能很好地处理闰年、夏令时、时区或所有其他与日期和时间相关的奇怪信息.

Using "magic numbers" and doing a lot of math with dates makes your code less readable, and also can cause problems - it doesn't deal well with leapyears, daylight savings time, time zones, or all the other weirdness that comes with dates and time.

在可能的情况下,您应该始终使用诸如 strtotime() 之类的辅助函数来处理日期并避免直接进行数学运算.

When possible, you should always use helper functions like strtotime() to deal with dates and avoid doing math directly.

这是我做你想做的事,它并不完美(你不能添加一个大于两个时间段之间的间隔的时间段 - 即从 18:30 到 8:30 的 14 小时 - 它仍然使用一些原始数学,但这是一个改进:

Here's my shot at doing what you want, it's not perfect (you can't add a time period larger than the gap between two periods - that is, 14 hours from 18:30 to 8:30 - and it still uses some raw math, but it's an improvement:

<?php
    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;
    }
?>

我已经注释掉了我用于调试的打印语句,如果你想看看它是如何工作的,你可以取消注释它们(我推荐!)你这样使用它:

I've commented out the print statements I had for debugging, you can uncomment them if you want to see how it works (which I recommend!) You use it as such:

<?php
    print date('Y-m-d H:i:s',addRollover('2013-01-01 17:30','2 hours')) . "\n";
    print date('Y-m-d H:i:s',addRollover('2013-01-01 18:00','3 hours')) . "\n";
    print date('Y-m-d H:i:s',addRollover('2013-01-04 17:00','150 minutes')) . "\n";
?>

结果:

2013-01-02 09:30:00
2013-01-02 11:00:00
2013-01-07 09:30:00

这段代码应该可以很好地处理大多数奇怪的日期,例如时区、夏令时等.使用 PHP 的 DateTime 类 会更好地实现,但这相当新的,我不太熟悉,所以我坚持使用 strtotime().

This code should deal just fine with most weirdnesses of dates, such as timezones, daylight savings, and such. It would be even better implemented with PHP's DateTime class, but that is fairly new and I'm not as familiar with it, so I stuck with strtotime().

这篇关于将两个小时添加到指定小时内的日期,该日期会滚动到下一个时间范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆