php 获取某个月的周次信息

在做统计的时候如果按照周统计 ,需要对某个月的周次信息进行计算,如果本月头一天不是星期一,则向上一个月取周一,本月最后的几天如果不能正好是一周,则忽略。

例如

2019-09月计算出来的结果

2016-08-29---2016-09-04
2016-09-05---2016-09-11
2016-09-12---2016-09-18
2016-09-19---2016-09-25

具体代码实现如下:

<?php
$current_year=2016;
$current_month=9;

$firstday = strtotime($current_year.'-'.$current_month.'-01');
//计算本月头一天的星期一
$monday=$firstday-86400*(date('N',$firstday)-1);//计算第一个周一的日期
//由于每个月只有四周 让 $i 从 1 到 5 增加即可
for ($i=1; $i <= 5; $i++) {
    $start=date("Y-m-d",$monday+($i-1)*86400*7);//起始周一
    $end=date("Y-m-d",$monday+$i*86399*7);//结束周日
    if(date('m',$monday+$i*86399*7)!=$current_month)
    {
        continue;
    }
    echo $start.'---'.$end."<br/>";//开始结束放入数组
}
?>

 

原文地址:https://www.cnblogs.com/lizhaoyao/p/5913841.html