一开始为了解决这个问题,在网上找了一些代码来使用。
结果在线上使用后,发现在跨年的时候会存在一些问题:1、某些年份为53周,但网上均为52周;2、周的开始时间可能是上一年的日期,而网上只能是1日。
只好自己撸代码了,有需要同学,请自取。
/**
* 获取某年指定周的开始日期和结束日期
* @param int $year 年份
* @param int $week 第几周
*/
function weekStartEndDateTwo($year,$week=1) {
$weekday = [
'start' => null,
'end' => null,
'date_pos' => null
];
//去年年份
$lastYear = intval($year)-1;
//去年有几周
$lastYearWeekNum = intval(date('W', strtotime( $lastYear . '-12-31')));
//去年最后一周区间
$lastYearWeek = weekStartEndDate($lastYear, $lastYearWeekNum);
if($lastYearWeekNum >= 52) {
$start = strtotime('+1 day', strtotime($lastYearWeek['end']));
} else {
$start = strtotime($lastYearWeek['start']);
}
// 第几周的开始时间
if ($week === 1){
$weekday['start'] = $start;
}else{
$weekday['start'] = strtotime('+'.($week-0).' monday',$start);
}
// 第几周的结束时间
$weekday

本文介绍了PHP编程中如何正确计算指定年份的指定周的开始日期和结束日期,特别关注了跨年和53周的情况,提供了自定义解决方案。
最低0.47元/天 解锁文章
3440

被折叠的 条评论
为什么被折叠?



