php 计算某一年某个月有几周
发布网友
发布时间:2022-04-06 01:54
我来回答
共1个回答
热心网友
时间:2022-04-06 03:23
<?php
function get_weekinfo($month){
$weekinfo = array();//创建一个空数组
$end_date = date('d',strtotime($month.' +1 month -1 day'));//计算当前月有多少天
for ($i=1; $i <$end_date ; $i=$i+7) { //循环本月有多少周
$w = date('N',strtotime($month.'-'.$i)); //计算第一天是周几
$weekinfo[] = array(date('Y-m-d',strtotime($month.'-'.$i.' -'.($w-1).' days')),date('Y-m-d',strtotime($month.'-'.$i.' +'.(7-$w).' days')));
} //当周开始时间 //结束时间
return $weekinfo;
}
print_r(get_weekinfo('2017-5'));
//执行结果
Array
(
[0] => Array
(
[0] => 2017-05-01
[1] => 2017-05-07
)
[1] => Array
(
[0] => 2017-05-08
[1] => 2017-05-14
)
[2] => Array
(
[0] => 2017-05-15
[1] => 2017-05-21
)
[3] => Array
(
[0] => 2017-05-22
[1] => 2017-05-28
)
[4] => Array
(
[0] => 2017-05-29
[1] => 2017-06-04
)
)
?>
代码参考的大神的,我加了下备注