PHP 程序用于查找两个给定日期范围内每周的天数
phpserver side programmingprogramming更新于 2025/8/25 2:37:17
要在 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"的数组会递增,并根据迭代过程中遇到的次数递增一周天数。

