Get date range between two dates excluding weekends

后端 未结 4 1715
情话喂你
情话喂你 2020-11-29 06:16

Given the following dates:

6/30/2010 - 7/6/2010

and a static variable:

$h = 7.5

I need to create an array

4条回答
  •  渐次进展
    2020-11-29 06:54

    This is OOP approach, just in case. It returns an array with all of dates, except the weekends days.

        class Date{
            public function getIntervalBetweenTwoDates($startDate, $endDate){
                $period = new DatePeriod(
                     new DateTime($startDate),
                     new DateInterval('P1D'),
                     new DateTime($endDate)
                );
                $all_days = array();$i = 0;
                foreach($period as $date) {
                    if ($this->isWeekend($date->format('Y-m-d'))){
                        $all_days[$i] = $date->format('Y-m-d');
                        $i++;
                    }
                }
                return $all_days;
            }
            public function isWeekend($date) {
                $weekDay = date('w', strtotime($date));
                if (($weekDay == 0 || $weekDay == 6)){
                    return false;
                }else{
                    return true;
                }
            }
        }
        $d = new Date();
        var_dump($d->getIntervalBetweenTwoDates('2015-08-01','2015-08-08'));
    

提交回复
热议问题