Home  >  Article  >  Backend Development  >  How to calculate all months in the past year in php?

How to calculate all months in the past year in php?

怪我咯
怪我咯Original
2017-07-10 09:44:502745browse

This article mainly introduces examples of PHP calculation of all months in the past year. Has very good reference value. Let’s take a look with the editor below

Without further ado, please look at the code:

$z = date('Y-m');
$a = date('Y-m', strtotime('-12 months'));
$begin = new DateTime($a);
$end = new DateTime($z);
$end = $end->modify('+1 month');
$interval = new DateInterval('P1M');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){ 
  echo $date->format("Y-m") . "<br />";
}

Rendering:

The above is the detailed content of How to calculate all months in the past year in 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