#native_company# #native_desc#
#native_cta#

Working days function

By Oliver Cronk
on March 4, 2002

Version: 0.8a

Type: Function

Category: Calendars/Dates

License: Artistic License

Description: Quick and dirty function to return the day, month and year adjusted for adding x amount of working days (i.e. it works out what is a working day and what isn’t). It is quick and dirty can could probably use a re-write…

//function to display the target dates for response NOTE that PHP lib functions use US MM-DD-YY format.

function ccs_calctarget($inday, $inmonth, $inyear, $req = 2)
{  if (!checkdate($inmonth, $inday, $inyear)) return (-1); // if input date is invalid then return -1
       
    $i = $req;
    do {
        if (checkdate($inmonth, $inday+1, $inyear)) { // If we can increment the day by 1 then do it
            $inday++;
            // if the day incremented is a working day (mon- friday) then increment $i
            $new = gmdate ("w", gmmktime (1, 1, 1, $inmonth, $inday, $inyear));  // Return the numerical 0-6 day of week from the date
            if(($new > 0) && ($new < 6)) { // if the day of week is greater than sunday (0) and less than saturday (6) then increment the day iterator
                    $i--;
            }

        } else if (checkdate($inmonth+1, 1, $inyear)) { // If not try increment the month and resetting day to 1
            $inday = 1;
            $inmonth++;
            // if the day incremented is a working day (mon- friday) then increment $i
            $new = gmdate ("w", gmmktime (1, 1, 1, $inmonth, $inday, $inyear));  // Return the numerical 0-6 day of week from the date
            if(($new > 0) && ($new < 6)) { // if the day of week is greater than sunday (0) and less than saturday (6) then increment the day iterator
                    $i--;
            }

        } else if (checkdate(1, 1, $inyear+1)) { // If not try increment the year and resetting day and month to 1
            $inday = 1;
            $inmonth = 1;
            $inyear++;
            // if the day incremented is a working day (mon- friday) then increment $i
            $new = gmdate ("w", gmmktime (1, 1, 1, $inmonth, $inday, $inyear));  // Return the numerical 0-6 day of week from the date
            if(($new > 0) && ($new < 6)) { // if the day of week is greater than sunday (0) and less than saturday (6) then increment the day iterator
                    $i--;
            }

        } else return (-1); // fail as we can go any further for some reason
    } while ($i != 0);
    
    // Check the final date return -1 if probs
    if (!checkdate($inmonth, $inday, $inyear)) return (-1);
    // reconstruct the date:
    $finaldate[0] = $inday;
    $finaldate[1] = $inmonth;
    $finaldate[2] = $inyear;
    return ($finaldate); // return the final date incremented by the amount of working days
}