I'm trying to get an array of 12 months starting with current month, decrement and be like "Mar 2022".
This is my code:
$months = array();
$count = 0;
while ($count <= 11) {
$months[] = date('M Y', strtotime("-".$count." month"));
$count++;
}
But has some problems with months with fewer days.
For example: dd($months[0]) => "Mar 2022"
and dd($months[1]) => "Mar 2022"
had to be "Feb 2022".
My Answer:
php
$currentMonth = date('F');
$months = array();
for ($i = 0; $i < 12; $i++) {
$months[] = date('F', strtotime("-$i month"));
}
print_r($months);
Output:
Array
(
[0] => November
[1] => October
[2] => September
[3] => August
[4] => July
[5] => June
[6] => May
[7] => April
[8] => March
[9] => February
[10] => January
[11] => December
)
Rate this post
3 of 5 based on 3893 votesComments