要查找PHP中两个给定日期范围之间每周的天数,代码如下-
<?php $start = "11-11-2019"; $end = "12-12-2019"; $week_day = array('Monday' => 0, 'Tuesday' => 0, 'Wednesday' => 0, 'Thursday' => 0, 'Friday' => 0, 'Saturday' => 0, 'Sunday' => 0); $start = new DateTime($start); $end = new DateTime($end); while($start <= $end ) { $time_stamp = strtotime($start->format('d-m-Y')); $week = date('l', $time_stamp); $week_day[$week] = $week_day[$week] + 1; $start->modify('+1 day'); } print_r("The number of days between the given range is"); print_r($week_day); ?>
输出结果
The number of days between the given range isArray ( [Monday] => 5 [Tuesday] => 5 [Wednesday] => 5 [Thursday] => 5 [Friday] => 4 [Saturday] => 4 [Sunday] => 4 )
定义了两个“ DateTime”类型的日期,并定义了一周中的几天的数组,其中最初一周中所有天的计数为0。日期被转换为时间格式,并为其分配了timestamp变量。一个名为“ week_day”的数组将递增,并根据迭代过程中遇到的次数来递增一周中的某几天。