0
$months = array();
$first = strtotime("first day this month");
for ( $i = 1; $i <= 6; $i++ ) {
    array_push($months, date('F', strtotime("-$i month", $first)));
}
var_dump($months);

This code is not working properly when run today (in march)

Output:

array(6) {
  [0]=>
  string(5) "March"
  [1]=>
  string(7) "January"
  [2]=>
  string(8) "December"
  [3]=>
  string(8) "November"
  [4]=>
  string(7) "October"
  [5]=>
  string(9) "September"
}

Where is February?

morissette
  • 1,071
  • 1
  • 8
  • 29

1 Answers1

0

Change your code into this to see what actually happens:

$months = array();
$first = strtotime("first day this month");
echo date('d-F-Y', $first);
for ( $i = 1; $i <= 6; $i++ ) {
    array_push($months, date('d-F-Y', strtotime("-$i month", $first)));
}
var_dump($months);

You will see that your $first date is not right. It is set at 29-March-2014. Why is February not there? Because it only has 28 days :)

Solution is: $first = strtotime("first day of this month"); which outputs 01-March-2014.

Saeverix
  • 397
  • 5
  • 18