Home >> PHP >> Get the last 12 months from current month by loop in PHP

Get the last 12 months from current month by loop in PHP

Example:-1

When current month is march.

$month[0]=date('m');
for ($i = 1; $i<12; $i++) {
      $month[$i]=date('m',strtotime(date('Y-m-01')." -$i month"));
}
print_r($month);
die();

Output

Array
(
    [0] => 03
    [1] => 02
    [2] => 01
    [3] => 12
    [4] => 11
    [5] => 10
    [6] => 09
    [7] => 08
    [8] => 07
    [9] => 06
    [10] => 05
    [11] => 04
)

Post Your Comment

Next Questions
How to list last 6 months from current month by loop
Get the last 6 months from current month by loop
Differentiate between variables and constants
How to increase maximum execution time
How to Increase File Upload Size
Set maximum execution time
Set maximum execution time unlimited
What are the rules for naming a variable
How can you compare objects
Some of the popular frameworks
How to concatenate two strings
How to encrypt password

Copyright ©2022 coderraj.com. All Rights Reserved.