Calculate business days

后端 未结 30 1865
猫巷女王i
猫巷女王i 2020-11-22 05:16

I need a method for adding \"business days\" in PHP. For example, Friday 12/5 + 3 business days = Wednesday 12/10.

At a minimum I need the code to understand weekend

30条回答
  •  旧时难觅i
    2020-11-22 06:02

    Here is a function for adding buisness days to a date

     function add_business_days($startdate,$buisnessdays,$holidays,$dateformat){
      $i=1;
      $dayx = strtotime($startdate);
      while($i < $buisnessdays){
       $day = date('N',$dayx);
       $date = date('Y-m-d',$dayx);
       if($day < 6 && !in_array($date,$holidays))$i++;
       $dayx = strtotime($date.' +1 day');
      }
      return date($dateformat,$dayx);
     }
    
     //Example
     date_default_timezone_set('Europe\London');
     $startdate = '2012-01-08';
     $holidays=array("2012-01-10");
     echo '

    Start date: '.date('r',strtotime( $startdate)); echo '

    '.add_business_days($startdate,7,$holidays,'r');

    Another post mentions getWorkingDays (from php.net comments and included here) but I think it breaks if you start on a Sunday and finish on a work day.

    Using the following (you'll need to include the getWorkingDays function from previous post)

     date_default_timezone_set('Europe\London');
     //Example:
     $holidays = array('2012-01-10');
     $startDate = '2012-01-08';
     $endDate = '2012-01-13';
     echo getWorkingDays( $startDate,$endDate,$holidays);
    

    Gives the result as 5 not 4

    Sun, 08 Jan 2012 00:00:00 +0000 weekend
    Mon, 09 Jan 2012 00:00:00 +0000
    Tue, 10 Jan 2012 00:00:00 +0000 holiday
    Wed, 11 Jan 2012 00:00:00 +0000
    Thu, 12 Jan 2012 00:00:00 +0000
    Fri, 13 Jan 2012 00:00:00 +0000 
    

    The following function was used to generate the above.

         function get_working_days($startDate,$endDate,$holidays){
          $debug = true;
          $work = 0;
          $nowork = 0;
          $dayx = strtotime($startDate);
          $endx = strtotime($endDate);
          if($debug){
           echo '

    get_working_days

    '; echo 'startDate: '.date('r',strtotime( $startDate)).'
    '; echo 'endDate: '.date('r',strtotime( $endDate)).'
    '; var_dump($holidays); echo '

    Go to work...'; } while($dayx <= $endx){ $day = date('N',$dayx); $date = date('Y-m-d',$dayx); if($debug)echo '
    '.date('r',$dayx).' '; if($day > 5 || in_array($date,$holidays)){ $nowork++; if($debug){ if($day > 5)echo 'weekend'; else echo 'holiday'; } } else $work++; $dayx = strtotime($date.' +1 day'); } if($debug){ echo '

    No work: '.$nowork.'
    '; echo 'Work: '.$work.'
    '; echo 'Work + no work: '.($nowork+$work).'
    '; echo 'All seconds / seconds in a day: '.floatval(strtotime($endDate)-strtotime($startDate))/floatval(24*60*60); } return $work; } date_default_timezone_set('Europe\London'); //Example: $holidays=array("2012-01-10"); $startDate = '2012-01-08'; $endDate = '2012-01-13'; //broken echo getWorkingDays( $startDate,$endDate,$holidays); //works echo get_working_days( $startDate,$endDate,$holidays);

    Bring on the holidays...

提交回复
热议问题