I need the previous six months list and am using the following code for that.
for ($i=6; $i >= 1; $i--) {
array_push($months, date('M', strtotime('-'.$i.' Month')));
}
print_r($months);
Its gives the wrong output as follows
Array
(
[0] => 'Dec'
[1] => 'Dec'
[2] => 'Jan'
[3] => 'Mar'
[4] => 'Mar'
[5] => 'May'
)
It must be
Array
(
[0] => 'Nov'
[1] => 'Dec'
[2] => 'Jan'
[3] => 'Feb'
[4] => 'Mar'
[5] => 'Apr'
)
Where am i wrong. Help please
You need to start the calculation from the first day of the month.
$first = strtotime('first day this month');
$months = array();
for ($i = 6; $i >= 1; $i--) {
array_push($months, date('M', strtotime("-$i month", $first)));
}
print_r($months);
/*
Array
(
[0] => Nov
[1] => Dec
[2] => Jan
[3] => Feb
[4] => Mar
[5] => Apr
)
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With