Fork me on GitHub

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

  

/*传入年份($current_year)和月份($current_month)*/
//获取某年某月的周次信息
public static function getWeekData($current_year, $current_month)
{
$weekData = [];
$firstday = strtotime($current_year . '-' . $current_month . '-01');
$monday = $firstday - 86400 * (date('N', $firstday) - 1);//计算第一个周一的日期
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;
}
$data = [$start, $end];
array_push($weekData, $data);
}
return $weekData;
}
posted @ 2021-12-09 18:00  君乐豹  阅读(186)  评论(0编辑  收藏  举报
Live2D