Last six months list in PHP
php
Solution
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
)
*/
Problem
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