PHP strtotime: Get previous month [duplicate]

和自甴很熟 提交于 2019-12-22 01:37:42

问题


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


回答1:


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)




回答2:


strtotime("-1 months") would be 2012-11-31, but there's no November, 31st. It is one day past 2012-11-30, which gives 2012-12-01. You will see it, when you do

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

gives as output

2012-12-01

See codepad



来源:https://stackoverflow.com/questions/14102625/php-strtotime-get-previous-month

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!