Home  >  Article  >  Backend Development  >  Get week information of a certain month based on PHP

Get week information of a certain month based on PHP

墨辰丷
墨辰丷Original
2018-06-07 16:22:211625browse

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(&#39;t&#39;,$stimestamp);
  $msdate=date(&#39;Y-m-d&#39;,$stimestamp);
  $medate=date(&#39;Y-m-&#39;.$mdays,$stimestamp);
 $etimestamp = strtotime($medate);
 //獲取第一周
 $zcsy=6-date(&#39;w&#39;,$stimestamp);//第一周去掉第一天還有幾天
 $zcs1=$msdate;
 $zce1=date(&#39;Y-m-d&#39;,strtotime("+$zcsy day",$stimestamp));
 $ret[1]="第1周".$zcs1.&#39;~&#39;.$zce1;
 //獲取中間周次
 $jzc=0;
 //獲得當前月份是6周次還是5周次
 $jzc0="";
 $jzc6="";
 for($i=$stimestamp; $i<=$etimestamp; $i+=86400){
 if(date(&#39;w&#39;, $i) == 0){$jzc0++;}
 if(date(&#39;w&#39;, $i) == 6){$jzc6++;}
 }
 if($jzc0==5 && $jzc6==5)
 {
 $jzc=5;
 }else{
 $jzc=4;
 }
 date_default_timezone_set(&#39;PRC&#39;);
 $t = strtotime(&#39;+1 monday &#39;.$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.&#39;~&#39;.$dedate;
 }
 //獲取最後一周
 $zcsy=date(&#39;w&#39;,$etimestamp);//最後一周是周幾日~六 0~6
 $zcs1=date(&#39;Y-m-d&#39;,strtotime("-$zcsy day",$etimestamp));
 $zce1=$medate;
 $jzcz=$jzc+1;
 $ret[$jzcz]="第".$jzcz."周".$zcs1.&#39;~&#39;.$zce1;
 return $ret;
}
$ret=getMonthweeks(&#39;2012-12-01&#39;);
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!

Statement:
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