2

I am trying to calculate number of week days (monda-friday) in a given date range using PHP. If the user enters the start (2010-12-01) and end date (2010-12-24), the function should spit out number of week days within that date range.

This is what I found but it doesnt help..

function getworkingdays_time($STARTSTAMP, $ENDSTAMP){

  $noofdays = ceil(($ENDSTAMP - $STARTSTAMP) / 86400);
    $sundaycounter = 0;
    $saturdaycounter = 0;
    $holidaycounter = 0;
    $offset = 0;
    $holidaycounter = check_holiday_range($STARTSTAMP, $ENDSTAMP);        // check holiday range


if(!defined("WORKSATURDAYS"))
{
    define("WORKSATURDAYS", 0);
}
if(!defined("WORKSUNDAYS"))
{
    define("WORKSUNDAYS", 0);
}  


    for ($i = 0; $i <= $noofdays; $i++){
        $ts = $STARTSTAMP + ($i * 86400);
        if (WORKSATURDAYS == '0' && date("l", $ts) == "Saturday"){
            ## they dont work saturdays
            $saturdaycounter ++;
        } elseif (WORKSUNDAYS == '0' && date("l", $ts) == "Sunday"){
            ## they dont work sundays
            $sundaycounter ++;
        }        
    }
    $total = $holidaycounter + $saturdaycounter + $sundaycounter;
    $offset = $total;
    if ($total > 0){
        $total = getworkingdays_time($ENDSTAMP + 86400, ($ENDSTAMP + ($total * 86400) + 86400));
        $offset += $total;
    }
    unset($holidaycounter, $saturdaycounter, $sundaycounter, $i, $noofdays, $ts, $total);
    return ($offset);
}

Any help will be greatly appreciated. Thanks heaps PSi

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
PSi
  • 21
  • 2

2 Answers2

2

Probably the easiest way to do this is to split the problem into three parts. Assuming that "week days" are Monday through Friday, then:

  1. Count the number of week days between now and the first Monday in the range. If the starting date is Monday, then this step supplies 0.
  2. Count backwards from the ending date to the last Monday in the range. If the ending date is on a Monday, then this step supplies 0.
  3. Determine the number of days between the first Monday and the last Monday, divide by 7, and multiply by 5.

Add the results of the three steps and you have the total number of week days in the given date range.

For holidays, you're best off computing the number of holidays in the range and subtracting from the total.

If you're working Saturdays or Sundays, then adjust the counting in the first two steps, and use a number other than 5 for the multiplicand once you've determined how many full weeks are in the range.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
0

This is a simplistic algorithm I came up with. Sorry, it's in Python, but it could be adaptable to other languages I believe. The arguments to the function (start, end) are expected to be time tuples.

def find_work_days(start,end):
    days = (end.tm_yday - start.tm_yday) + 1
    weeks = days / 7
    leftover = days % 7
    if start.tm_wday + leftover == 6:
        leftover -= 1
    elif start.tm_wday + leftover > 6:
        leftover -= 2
    if start.tm_wday == 6: leftover += 1

    work_days = weeks * 5 + leftover
    return work_days
dustin
  • 4,309
  • 12
  • 57
  • 79