php按指定日期范围获取周次,周次时间范围

获取周次

functiongetWeekNoToFirstDay($time){$iFirstWeekDay=date('w',strtotime(date("Y",$time).'-01-01'));if($iFirstWeekDay!=1){//指定年的1月1号不是星期一if($iFirstWeekDay==){//星期天if(date("Y-m-d",$time)==date("Y",$time).'-01-01'){//如果1号为周日,并且获取传入的日期也是 1号。$iFirstWeekDay=7;$sWeekNo=strftime("%W",strtotime(date("Y-m-d",$time)));}else{$iFirstWeekDay=7;$sWeekNo=strftime("%W",strtotime(date("Y-m-d",$time)))+1;}}else{$sWeekNo=strftime("%W",strtotime(date("Y-m-d",$time)))+1;}$iAdjustDay=(8-$iFirstWeekDay)+($sWeekNo-1)*7;if($iAdjustDay

获取周次范围:

functiongetWeekNoToDateRange($time,$is_timestamp=false){$dateTemp=getWeekNoToFirstDay($time);if($is_timestamp){$sDateRange['start_week_day']=$dateTemp;$sDateRange['end_week_day']=strtotime("+7 day",$dateTemp);}else{$sDateRange['start_week_day']=date('Y-m-d H:i:s',$dateTemp);$sDateRange['end_week_day']=date('Y-m-d H:i:s',strtotime("+7 day",$dateTemp));}return$sDateRange;}

获取指定日期范围一共有多少周

functiongetWeekNoToTotalWeeks($start_time,$end_time){if(!$start_timeor!$end_time){returnnull;}$time_from_yearweek=getWeekNoToFirstDay($start_time);$time_end_yearweek=getWeekNoToFirstDay($end_time);$total_weeks=($time_end_yearweek-$time_from_yearweek)/86400/7;if($total_weeks>=){$total_weeks++;}return$total_weeks;}

使用:

时间:2023-01-01 00:00:00 周日

Array ( [start_week_day] => 2022-12-26 00:00:00 [end_week_day] => 2023-01-02 00:00:00 )

时间:2023-01-02 00:00:00 周一

Array ( [start_week_day] => 2023-01-02 00:00:00 [end_week_day] => 2023-01-09 00:00:00 )

取时间:2022-12-31 00:00:00

Array ( [start_week_day] => 2022-12-26 00:00:00 [end_week_day] => 2023-01-02 00:00:00 )

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180825G1JS6700?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 yunjia_community@tencent.com 删除。

扫码关注云+社区

领取腾讯云代金券