SoFunction
Updated on 2025-03-10

PHP implements a method to obtain weekly information for a certain month

This article describes the method of PHP to obtain weekly information for a certain month. Share it for your reference. The details are as follows:

<?php
 function getMonthweeks($date){
 $ret=array();
  $stimestamp=strtotime($date);
  $mdays=date('t',$stimestamp);
  $msdate=date('Y-m-d',$stimestamp);
  $medate=date('Y-m-'.$mdays,$stimestamp);
 $etimestamp = strtotime($medate);
 //Get the first week $zcsy=6-date('w',$stimestamp);//How many days will it take to remove the first day in the first week $zcs1=$msdate;
 $zce1=date('Y-m-d',strtotime("+$zcsy day",$stimestamp));
 $ret[1]="Week 1".$zcs1.'~'.$zce1;
 //Get the middle week $jzc=0;
 //The current month was 6 weeks or 5 weeks $jzc0="";
 $jzc6="";
 for($i=$stimestamp; $i<=$etimestamp; $i+=86400){
 if(date('w', $i) == 0){$jzc0++;}
 if(date('w', $i) == 6){$jzc6++;}
 }
 if($jzc0==5 && $jzc6==5)
 {
 $jzc=5;
 }else{
 $jzc=4;
 }
 date_default_timezone_set('PRC');
 $t = strtotime('+1 monday '.$msdate);
 $n = 1;
 for($n=1; $n<$jzc; $n++) {
 $b = strtotime("+$n week -1 week", $t);
 $dsdate=date("Y-m-d", strtotime("-1 day", $b));
 $dedate=date("Y-m-d", strtotime("5 day", $b));
 $jzcz=$n+1;
 $ret[$jzcz]="Third".$jzcz."week".$dsdate.'~'.$dedate;
 }
 //Get the last week $zcsy=date('w',$etimestamp);//The last week is a few days~Saturdays 0~6 $zcs1=date('Y-m-d',strtotime("-$zcsy day",$etimestamp));
 $zce1=$medate;
 $jzcz=$jzc+1;
 $ret[$jzcz]="Third".$jzcz."week".$zcs1.'~'.$zce1;
 return $ret;
}
$ret=getMonthweeks('2012-12-01');
for($i=0; $i<=count($ret); $i++) {
 echo @$ret[$i]."<br />";
}
?>

The operation results are as follows:

Week 12-12-01~2012-12-01
Week 22012-12-02~2012-12-08
Week 3 2012-12-09~2012-12-15
Week 4 2012-12-16~2012-12-22
Week 52012-12-23~2012-12-29
Week 62012-12-30~2012-12-31

I hope this article will be helpful to everyone's PHP programming.