Days calc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geraldjr30
    New Member
    • Jan 2009
    • 60

    #1

    Days calc

    hi,

    i have the following:

    Code:
    <?php
    echo"test";
    //The function returns the no. of business days between two dates and it skips the holidays
    function getWorkingDaysTOT($startDate2,$endDate2,$HOLS){
        //The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24
        //We add one to inlude both dates in the interval.
        $days2 = (strtotime($endDate2) - strtotime($startDate2)) / 86400+1; //used for calculating elapsed bus. days
        
        $no_full_weeks2 = floor($days2 / 7);
        $no_remaining_days2 = fmod($days2, 7);
    
        
        //It will return 1 if it's Monday,.. ,7 for Sunday
        $the_first_day_of_week2 = date("N",strtotime($startDate2));
        $the_last_day_of_week2 = date("N",strtotime($endDate2));
    
        //---->The two can be equal in leap years when february has 29 days, the equal sign is added here
        //In the first case the whole interval is within a week, in the second case the interval falls in two weeks.
        if ($the_first_day_of_week2 <= $the_last_day_of_week2){
            if ($the_first_day_of_week2 <= 6 && 6 <= $the_last_day_of_week2) $no_remaining_days2--;
            if ($the_first_day_of_week2 <= 7 && 7 <= $the_last_day_of_week2) $no_remaining_days2--;
        }
        else{
            if ($the_first_day_of_week2 <= 6) {
            //In the case when the interval falls in two weeks, there will be a Sunday for sure
                $no_remaining_days2--;
            }
        }
    
        //The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder
    //---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it
       $workingDays2 = $no_full_weeks2 * 5;
        if ($no_remaining_days2 > 0 )
        {
          $workingDays2 += $no_remaining_days2;
        }
    
    
    
    
    
    
    
        //We subtract the holidays
        foreach($HOLS as $HOL){
            $time_stamp2=strtotime($HOL);
            //If the holiday doesn't fall in weekend
            if (strtotime($startDate2) <= $time_stamp2 && $time_stamp2 <= strtotime($endDate2) && date("N",$time_stamp2) != 6 && date("N",$time_stamp2) != 7)
                $workingDays2--;
        }
    
        return $workingDays2;
    
    }
    
    ?>
    
    
    
    
    
    
    
    
    
    <?php
      $c = 1;
      //get timestamp for past/future date I want
      $pf_time2 = strtotime("-".$c." day");
      //format the date using the timestamp generated
      $previous_day2 = date("d M Y", $pf_time2);
    ?>
    
    
    
    
    <?php
    $HOLS=array("25 Dec 2008","26 Dec 2008","16 Feb 2009");
    
    echo getWorkingDaysTOT("2009-02-15","2009-02-21",$HOLS);
    
    $days_in_month2 = date("t"); 
    $elapsed_business_days2 =  getWorkingDaysTOT(date("1 M Y"),$previous_day2,$HOLS); //gets the number of working days elapsed in the current month minus any holidays
    $business_days2 = getWorkingDaysTOT(date("1 M Y"),date("$days_in_month2 M Y"),$HOLS);
    
    
    
    echo "<br>";
    echo "business days:";
    echo $business_days2;
    
    
    ?>
    for some reason it only works right if i restart the apache server. for example, before i restart the server i am getting 18 for business days. after i restart it i am getting 19 business days. 19 is the correct number. can someone tell me why this is happening, and how i can maybe simulate restarting server or something?

    thanks in advance,
    geebee
Working...