Home  >  Article  >  Backend Development  >  PHP获取当前月份的前一个月、后一个月

PHP获取当前月份的前一个月、后一个月

WBOY
WBOYOriginal
2016-06-23 13:08:242013browse

在某次开发中,需要对月份进行处理,获取到前一个月或者后一个月,开始使用

date("Ym", strtotime("-1 month")) 

后来发现,这种方法会有问题,在月份有31天的时候,比如7月31日,会出现 date("Ym", strtotime("-1 month")) 这个是时间也是201207与date("Ym")结果一样。这样就会导致在这天产生很多问题。



 后来只能用这样 的方法


Php代码  

function GetMonth($sign="1")  {      //得到系统的年月      $tmp_date=date("Ym");      //切割出年份      $tmp_year=substr($tmp_date,0,4);      //切割出月份      $tmp_mon =substr($tmp_date,4,2);      $tmp_nextmonth=mktime(0,0,0,$tmp_mon+1,1,$tmp_year);      $tmp_forwardmonth=mktime(0,0,0,$tmp_mon-1,1,$tmp_year);      if($sign==0){          //得到当前月的下一个月           return $fm_next_month=date("Ym",$tmp_nextmonth);              }else{          //得到当前月的上一个月           return $fm_forward_month=date("Ym",$tmp_forwardmonth);               }  }



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