Home  >  Article  >  Backend Development  >  PHP method to calculate all months in the past year

PHP method to calculate all months in the past year

墨辰丷
墨辰丷Original
2018-05-24 17:20:171475browse

This article mainly introduces the method of calculating all months in the past year in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

Without further ado, please take a 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 entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

JavaScript is used to set the month method setMonth()

php How to calculate all months in the past year?

#php method example code to determine how many months differ between two dates

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