PHP 程序,求出两个已知日期范围内的每周天数
为了用 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。这些日期被转换成时间格式,并分配时间戳变量。名为“week_day”的数组被增加,并根据在迭代中遇到的次数增加星期几的天数。
广告