Home > Backend Development > PHP Tutorial > PHP method to obtain week information of a certain month_php skills

PHP method to obtain week information of a certain month_php skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 20:08:34
Original
1022 people have browsed it

The example in this article describes the method of obtaining week information of a certain month in PHP. Share it with everyone for your reference. The details are as follows:

<&#63;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 />";
}
&#63;>

Copy after login

The running results are as follows:

Week 1 2012-12-01~2012-12-01
Week 2 2012-12-02~2012-12-08
Week 32012-12-09~2012-12-15
Week 42012-12-16~2012-12-22
Week 5 2012-12-23~2012-12-29
Week 6 2012-12-30~2012-12-31

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template