views:

60

answers:

1

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
+1  A: 

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.

salathe
Hi, thanks - I think I'm actually going to take a different route, I'll use the add_business_days function which works and hide weekends from the calendar view - same effect and less hassle.