1

嗨,我已经在使用另一个问题的代码 - 在周末的情况下,它会在结束日增加两天

function add_business_days($startdate,$buisnessdays,$holidays,$dateformat){
  $i=1;
  $dayx = strtotime($startdate);
  while($i < $buisnessdays){
   $day = date('N',$dayx);
   $datex = date('Y-m-d',$dayx);
   if($day < 6 && !in_array($datex,$holidays))$i++;
   $dayx = strtotime($datex.' +1 day');
  }
  return date($dateformat,$dayx);
 }

此函数构成 json 输出的一部分,该输出显示在 jquery 日历中 - 它获取 startdate 和 enddate 并呈现它。

是否可以创建一个返回输出的代码,这样当它到达周末时它会创建一个结束日期,跳到星期一创建一个开始日期然后继续直到它达到原始给定的结束日期?

x = date('w');
if (x != 6) {
while (x != 6) {
//start adding days to start date
}
} else {
//create new enddate = current iteration of dates currentdate;

//then new start (add two days to it to get to monday) currentdate + 2 = newstartdate
//redo above till you get to original end date
4

1 回答 1

2

我不是 100% 确定问题/功能真正在做什么,但(如果我猜对了)这是一个想法。

function add_business_days($startdate, $businessdays, $holidays, $dateformat)
{
    $start = new DateTime($startdate);
    $date  = new DateTime($startdate);
    $date->modify("+{$businessdays} weekdays");

    foreach ($holidays as $holiday) {
        $holiday = new DateTime($holiday);
        // If holiday is a weekday and occurs within $businessdays of the $startdate
        if ($holiday->format('N') < 6 && $holiday >= $start && $holiday <= $date) {
            $date->modify("+1 weekday");
        }
    }
    return $date->format($dateformat);
}

该逻辑基本上将$businessdays工作日添加到开始日期;然后检查日期范围内的任何假期,如果确实发生了任何假期,则最终日期会酌情增加。

于 2010-08-09T12:05:21.410 回答