PHP使用函数在给定的小时内添加两个小时 [英] PHP Add two hours to a date within given hours using function
问题描述
如何构建条件,仅在上午08:30至晚上18:30(不包括星期六和星期日)之间添加两个小时?
如果接近边界的时间(例如星期二17:30),则应将左边的时间加到下一个有效时间段的开头。
$ b例如:如果给定的日期是星期二的17:30,则两个小时的增加将导致周三的9:30(17:30 + 1小时= 18:30,8:30 +剩余的时间) 1小时= 9:30)。或者如果给定的日期是星期五17:00,结果将是星期一(17:00星期五+ 1.5小时= 18:30,星期一8:30 +剩余时间.5小时= 9:00)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; //开始时间(分钟)(小时* 60)
$ endtime = 18.5 * 60; //结束时间(分钟)(小时* 60)
$ givenDate = strtotime($ givenDate);
//获取给定时间的日期部分
$ givenDay = strtotime('today',$ givenDate);
//计算今天的结束是
$ maxToday = strtotime(+ $ endtime minutes,$ givenDay);
//计算下一个周期的开始
$ nextPeriod = strtotime(tomorrow,$ givenDay); //将其设置为第二天
$ nextPeriod = strtotime(+ $ starttime minutes,$ nextPeriod); //并添加起始时间
//如果是周末,将其冲击到星期一
if(date(D,$ nextPeriod)==Sat){
$ nextPeriod = strtotime(+ 2天,$ nextPeriod);
}
//将时间段添加到新的一天
$ newDate = strtotime(+ $ addtime,$ givenDate);
// print$ givenDate - > $ newDate\\\
;
// print$ maxToday\\\
;
//将新小时作为十进制(加上分钟/ 60)
$ hourfrac = date('H',$ newDate)+ date('i',$ newDate)/ 60;
// print$ hourfrac\\\
;
//检查我们是否在需要的范围之外
if($ hourfrac< $ starttime || $ hourfrac> $ endtime){
//我们是在范围之外,找到余数并将其添加到
$ remaining = $ newDate - $ maxToday;
// print$ remaining\\\
;
$ newDate = $ nextPeriod + $余数;
}
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->格式('G')||
(17 === $ datetime->格式('G')&& 30< $ datetime->格式('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小时');
echo $ future-> format('Y-m-d H:i:s');
以下是对发生的情况的解释:
-
首先我们创建一个表示我们的开始日期/时间的DateTime对象
-
然后我们添加指定数量的时间到(支持的日期和时间格式)
-
我们检查是否是一个周末,6PM后,或在超过30分钟(例如下午5:30之后)的下午5点的时间内,
-
如果是,我们克隆我们的datetime对象并将其设置为5:30 PM
-
然后我们得到差异在结束时间(下午5:30)和修改时间之间作为 DateInterval对象< a>
-
然后我们进入第二天
-
如果第二天我们星期六进入第二天
-
如果第二天是星期天,我们进入第二天
-
然后我们将时间设定为上午8:30。
-
然后,我们将结束时间(下午5: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?
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.
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;
i have tried this this function and it works great except when i use a date like ( 2013-11-26 12:30 ) he gives me ( 2013-11-27 04:30:00 ) the problem is with 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;
}
I don't know if you still need this but here it is anyway. Requires PHP 5.3 or higher
<?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');
Here's an explanation of what's going on:
First we create a DateTime object representing our starting date/time
We then add the specified amount of time to it (see Supported Date and Time Formats)
We check to see if it is a weekend, after 6PM, or in the 5PM hour with more than 30 minutes passed (e.g. after 5:30PM)
If so we clone our datetime object and set it to 5:30PM
We then get the difference between the end time (5:30PM) and the modified time as a DateInterval object
We then progress to the next day
If the next day is a Saturday we progress to the next day
If the next day is a Sunday we progress to the next day
We then set our time to 8:30AM
We then add our difference between the end time (5:30PM) and the modified time to our datetime object
We return the object from the function
这篇关于PHP使用函数在给定的小时内添加两个小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!