喜已经我使用这个代码从另一个问题 - 它增加了两个额外的天,周末的情况下endday指定

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);
 }

这个函数的形式被显示在一个jquery日历一个JSON输出的一部分 - 它拿起STARTDATE和结束日期和使得它

是否有可能创造一个代码,返回输出,使得当它到达它创建结束日期一个周末,跳到星期一创建一个开始日期然后继续直到它到达给定的结束日期??

的原始
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
有帮助吗?

解决方案

我不是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平日的开始日期;然后的时间范围内的任何假期检查,如果任何假期做发生那么最终日期递增适当。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top