This article mainly introduces how to obtain weekly information of a certain month based on PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
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); //獲取第一周 $zcsy=6-date('w',$stimestamp);//第一周去掉第一天還有幾天 $zcs1=$msdate; $zce1=date('Y-m-d',strtotime("+$zcsy day",$stimestamp)); $ret[1]="第1周".$zcs1.'~'.$zce1; //獲取中間周次 $jzc=0; //獲得當前月份是6周次還是5周次 $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]="第".$jzcz."周".$dsdate.'~'.$dedate; } //獲取最後一周 $zcsy=date('w',$etimestamp);//最後一周是周幾日~六 0~6 $zcs1=date('Y-m-d',strtotime("-$zcsy day",$etimestamp)); $zce1=$medate; $jzcz=$jzc+1; $ret[$jzcz]="第".$jzcz."周".$zcs1.'~'.$zce1; return $ret; } $ret=getMonthweeks('2012-12-01'); for($i=0; $i<=count($ret); $i++) { echo @$ret[$i]."<br />"; } ?>
The running results are as follows:
Week 1 2012-12-01~2012-12-01
2 weeks 2012-12-02~2012-12-08
3rd week 2012-12-09~2012-12-15
4th week 2012-12-16~2012-12-22
Week 5 2012-12-23~2012-12-29
Week 6 2012-12-30~2012-12-31
Summary: The above is this article The entire content, I hope it will be helpful to everyone's study.
Related recommendations:
php realizes curl upload, download, https login
PHP realizes page staticization Detailed explanation of several methods
PHP method to implement hexadecimal color random generator function
The above is the detailed content of Get week information of a certain month based on PHP. For more information, please follow other related articles on the PHP Chinese website!