检查日期范围内的周末数

时间:2016-07-07 12:11:14

标签: php laravel date range weekend

如何在Laravel中查看在$from$to之间的给定日期范围内有多少个星期六或星期日?

1 个答案:

答案 0 :(得分:1)

你可以使用普通的PHP这样做:

$start = new DateTime('2016-07-07');
$end = new DateTime('2016-07-27');

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($start, $interval ,$end);

$saturdays = 0;
foreach($daterange as $date){
    $days = $date->format('D');
    if ($days == 'Sat') {
        $saturdays++;
    }
}

echo $saturdays;