1

こんにちはすでに私は別の質問からこのコードを使用しています-週末の場合は終了日に2日余分に追加されます

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出力の一部を形成します。つまり、開始日と終了日を取得してレンダリングします。

週末になると終了日を作成し、月曜日にスキップして開始日を作成し、元の指定された終了日に達するまで続行するような出力を返すコードを作成することは可能ですか?

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 に答える