mysql-function to count days between 2 dates excluding weekends

前端 未结 4 1161
天涯浪人
天涯浪人 2020-12-16 07:06

I\'ve searched through many examples , good ones I got :

  1. Count days between two dates, excluding weekends (MySQL only)

  2. How to count date dif

4条回答
  •  别那么骄傲
    2020-12-16 07:10

    Had a similar issue, I used PHP to remove the weekends, need to know start day and number of days:

    EG SQL:

      SELECT DAYOFWEEK(`date1`) AS `startday`, TIMESTAMPDIFF(DAY, `date1`, `date2`) AS `interval` FROM `table`
    

    Then run the result through a PHP function:

        function noweekends($startday, $interval) {
            //Remove weekends from an interval
            $wecount = 0; $tmp = $interval;
            while($interval/7 > 1) { $interval-=7; $wecount++; }
            if($interval+$startday > 5) $wecount++;
            $interval = $tmp-($wecount*2);
            return $interval;
        }
    

提交回复
热议问题