给定日期的下一个工作日在PHP [英] Next business day of given date in PHP

查看:349
本文介绍了给定日期的下一个工作日在PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有PHP代码段来计算给定日期的下一个工作日?
例如,YYYY-MM-DD需要转换以找出下一个工作日吗?



示例:
对于03.04 .2011(DD-MM-YYYY)下一个工作日是04.04.2011。
对于08.04.2011下一个工作日是11.04.2011。



这是包含下一个工作日需要知道的日期的变量, p>

  $ cubeTime ['time']; 

变量包含:2011-04-01
代码片段的结果应为:2011- 04-04

解决方案

下一个工作日



找到从特定日期(不包括星期六或星期日)的下一个工作日:

  echo date('Ym-d ',strtotime('2011-04-05 +1 Weekday')); 

您还可以使用日期变量来执行:

  $ myDate ='2011-04-05'; 
echo date('Y-m-d',strtotime($ myDate。'+1 Weekday'));






想跳过假期? / strong>



虽然原来的海报提到我不需要考虑假期,如果你真的想忽略假期,只要记住 - 假期只是一系列不想包含的日期,不同国家,地区,公司,人员等。



只需将上述代码放入一个排除/循环超出您不希望包含的日期的功能。这样的东西:

  $ tmpDate ='2015-06-22'; 
$ holidays = ['2015-07-04','2015-10-31','2015-12-25'];
$ i = 1;
$ nextBusinessDay = date('Y-m-d',strtotime($ tmpDate。'+'。$ i。'Weekday'));

while(in_array($ nextBusinessDay,$ holidays)){
$ i ++;
$ nextBusinessDay = date('Y-m-d',strtotime($ tmpDate。'+'。$ i。'Weekday'));
}

如果您愿意,我相信上述代码可以简化或缩短。我试着用易于理解的方式写出来。


Does anyone have a PHP snippet to calculate the next business day for a given date? How does, for example, YYYY-MM-DD need to be converted to find out the next business day?

Example: For 03.04.2011 (DD-MM-YYYY) the next business day is 04.04.2011. For 08.04.2011 the next business day is 11.04.2011.

This is the variable containing the date I need to know the next business day for

$cubeTime['time'];

Variable contains: 2011-04-01 result of the snippet should be: 2011-04-04

解决方案

Next Weekday

This finds the next weekday from a specific date (not including Saturday or Sunday):

echo date('Y-m-d', strtotime('2011-04-05 +1 Weekday'));

You could also do it with a date variable of course:

$myDate = '2011-04-05';
echo date('Y-m-d', strtotime($myDate . ' +1 Weekday'));


Want to Skip Holidays?:

Although the original poster mentioned "I don't need to consider holidays", if you DO happen to want to ignore holidays, just remember - "Holidays" is just an array of whatever dates you don't want to include and differs by country, region, company, person...etc.

Simply put the above code into a function that excludes/loops past the dates you don't want included. Something like this:

$tmpDate = '2015-06-22';
$holidays = ['2015-07-04', '2015-10-31', '2015-12-25'];
$i = 1;
$nextBusinessDay = date('Y-m-d', strtotime($tmpDate . ' +' . $i . ' Weekday'));

while (in_array($nextBusinessDay, $holidays)) {
    $i++;
    $nextBusinessDay = date('Y-m-d', strtotime($tmpDate . ' +' . $i . ' Weekday'));
}

I'm sure the above code can be simplified or shortened if you want. I tried to write it in an easy-to-understand way.

这篇关于给定日期的下一个工作日在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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