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 at it with the editor.
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 />"; }
Effect picture:
The above is the detailed content of PHP calculates the code details of all months in the past 1 year. For more information, please follow other related articles on the PHP Chinese website!