PHP Strtotime -1month -2month

bcmcfc picture bcmcfc · Jul 31, 2009 · Viewed 39.1k times · Source

This was working fine yesterday with no changes to the code.

echo date("M", strtotime("-3 month", time()) );
echo date("M", strtotime("-2 month", time()) );
echo date("M", strtotime("-1 month", time()) );
echo date("M", time());

The output it was producing yesterday was as you would expect- i.e. Apr, May, Jun, Jul

Today it echoes May May Jul Jul

Any ideas?

Thanks in advance.

Answer

Pascal MARTIN picture Pascal MARTIN · Jul 31, 2009

It might be related to bug #44073

You could try with something like this :

echo date("M", strtotime("-3 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", strtotime("-2 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", strtotime("-1 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", time()) . "\n";

(Solution found in the comments section of strtotime ; direct link)

And the output :

Apr
May
Jun
Jul

Kind of "cheating" with the date format and month's name and all that...