开发者

PHP Business days stop at weekends create a new start date on monday

Hi already i'm using this code from another question - which adds two extra days to endday in the case of weekends

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

This function forms part of a json output which is displayed in a jquery calendar - it picks up startdate and enddate and renders it.

Is it possible to create a code that returns outputs such that when it gets to a weekend it creates an end date, skips to Monday creates a start date then continues till it reaches the original given 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


I'm not 100% sure about what the question/function is really doing but (if I guessed correctly) here's an idea.

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

The logic basically adds $businessdays weekdays to the start date; then checks for any holidays within the date range, if any holidays do occur then the final date is incremented as appropriate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜