简体   繁体   English

将两个工作日添加到日期数组中,不包括假日日期

[英]Add two business days to a date array excluding holiday dates

I am trying to add 2 weekdays to a date, but I also want to exclude the days in an array. 我想在工作日添加2个日期,但我也想排除数组中的日期。

array of dates to exclude: 要排除的日期数组:

$bankHolidays = array();
foreach($obj as $e) {
    if($e->division == 'england-and-wales') {
        foreach($e->events as $events) {
            $bankHolidays[] = $events->date;
        }
    }
}

Adding 2 working days to date 到目前为止增加2个工作日

$ret = date('d-m-Y', strtotime($bill["charge_customer_at"]. ' +2 weekdays'));

How can I include the array of dates to exclude? 如何包含要排除的日期数组?

As an example, if my date was 2017-07-19 and i want to add 2 week days, that would output 2017-07-21 . 例如,如果我的日期是2017-07-19并且我想添加2周的日期,那么将输出2017-07-21

But if 2017-07-21 was in my array, it should skip this date and continue adding 2 weekdays so the output would end up being 2017-07-24 because of the weekend as well. 但是如果2017-07-21在我的阵列中,它应该跳过这个日期并继续增加2个工作日,所以输出最终将是2017-07-24因为周末也是如此。

You could do something as simple as use a while loop. 你可以做一些像使用while循环一样简单的事情。

$date = '2017-07-25';
$reserved = ['2017-07-27', '2017-07-28'];
$days = 2;

while ($days > 0) {
    $date = date('Y-m-d', strtotime($date . ' +1 weekday'));
    if (! in_array($date, $reserved)) $days--;
}

var_dump($date);

Find the next business day function with a given offset. 找到具有给定偏移量的下一个工作日函数。

The following basically takes your given date, the number of days you want to skip, in your case 2, and the holidays array that you prepopulate as you show in your question. 以下内容基本上取决于您的给定日期,您想要跳过的天数,在您的案例2中,以及您在问题中显示时预先填充的假日数组。 If the weekday that's so many after your date is a holiday it adds a day and checks again. 如果工作日在您的约会之后是如此之多,那么它会增加一天并再次检查。

function nextBusinessDay($date, $daysToSkip,$holidays){
    $day = date('Y-m-d',strtotime($date. ' + '.$daysToSkip.' weekday'));
    if(!in_array($day,$holidays)){
         return $day;
    } else {
        return nextBusinessDay(date('Y-m-d',strtotime($date.' +1 day')), $daysToSkip,$holidays);
    }
}

$date = '2017-07-19';
$holidays = ['2017-07-21'];
echo nextBusinessDay($date,2,$holidays);//returns 2017-07-24

$date = '2017-07-19';
$holidays = ['2017-07-21', '2017-07-24'];
echo nextBusinessDay($date,2,$holidays);//returns 2017-07-25 like if it were a 4 day weekend

Sorry it took me a while to get a chance to look it over and post something. 对不起,我花了一段时间才有机会查看并发布一些内容。 This should work for you. 这应该适合你。 My understanding is that you need to return the first business date that is two days from a given date, not keep adding two week days until the date isn't in your holidays array. 我的理解是,您需要返回距离给定日期两天的第一个营业日期,而不是继续添加两个工作日,直到日期不在您的假期数组中。 If you really want to keep adding 2 days, or however many days, then change 如果你真的想继续增加2天,或者多天,那就改变吧

return nextBusinessDay(date('Y-m-d',strtotime($date.' +1 day')), $daysToSkip,$holidays);

to

return nextBusinessDay(date('Y-m-d',strtotime($date.' +'.$daysToSkip.' day')), $daysToSkip,$holidays);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM