PHP strtotime: Get previous month

Martin picture Martin · Dec 31, 2012 · Viewed 44.3k times · Source

Possible Duplicate:
How to get previous month and year relative to today, using strtotime and date?

Today is December, 31st but strtotime("-1 months") returns December:

echo date("Y-m", strtotime("-1 months"));

Same for strtotime("last month")

How can I properly return the previous month (November)?

Test: http://codepad.viper-7.com/XvMaMB

Answer

salathe picture salathe · Dec 31, 2012
strtotime("first day of last month")

The first day of is the important part as detailed on the Relative Formats manual page.


Example: http://codepad.viper-7.com/dB35q8 (with hard-coded today's date)