Hey guys, I was wondering if anyone had written a function that will return the number of work days between two dates.

For instance if I put in the dates 2008-04-19 and 2008-05-02 it would return the number of days between those two dates minus weekends. Well not just between, it would also include those two days.

Any input would be great, thanks.

    I figured this has been done many times, but I can't figure out how to go about it.

    I was thinking of looping thru the dates somehow and using the date function to check if that date is a Sat or Sun using the Date("l") function, but I am not sure how to loop thru dates because you also have to know how many days are in the month and then start in the new month if the dates cross between two months...etc.

    Anyone have any input? Thanks.

      you can increment a date pretty easily with the strtotime() function.
      $date = strtotime("+1 day", $date);

      just make sure $date is a timestamp and your set. check this page out for more details: http://us2.php.net/strtotime

        Thanks for the input. I am still unsure of exactly how to go about it though.

        For example, I am using this code to get the total number of days counting weekends, which is basicaly subtracting the first date from the second date using strtotime and then dividing by (606024).

        for($i=0;$i<$numRows;$i++){
        
          $anyRow = $db->getAnyRow($i);
        
          $days = (strtotime($anyRow['StartDate']) - strtotime($anyRow['OutDate])) / (60 * 60 * 24);
        
          $days=substr($days,1,strlen($days));
        
          $totalDays+=$days;
        
        }//end for
        
        

        Now I am trying to figure out how to do the same thing except subtract days that are Sat or Sun.

        For example

        for($i=0;$i<$numRows;$i++){
        
          $anyRow = $db->getAnyRow($i);
        
          $days = (strtotime($anyRow['StartDate']) - strtotime($anyRow['OutDate])) / (60 * 60 * 24);
        
         $firstDate = $anyRow['StartDate'];
        
        
        for($j=0;$j<$days;$j++){
        
          $theDate = strtotime("+".$j." day", $firstDate);
        
          if($theDate != "Sat" && $theDate != "Sun"){//Not sure how to tell if the date is a Sat or Sun
             $totalWorkDays+=1;
           }//end if
        }//end for
        
        }//end for
        
        

          Cool, I looked it over and although I am not using timestamps in the database, just regular dates, I think I can take from that code and make it work.

          Thanks!

          I'll come back and mark this thread resolved once I get it working and I'll post the code too.

            Write a Reply...