PHP日期返回错误月份减去一个月 [英] PHP date returning wrong Month on subtracting one month

查看:450
本文介绍了PHP日期返回错误月份减去一个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前日期是2017年3月29日

Current Date is 29th March 2017

当我使用PHP减去2个月,我得到 January

When I subtract 2 months using PHP and I get January

$prevmonth = date('M', strtotime('-2 months'));
echo $prevmonth;

但是当我减1个月后,它会给出 3月份

But when I subtract 1 month it gives March

$prevmonth = date('M', strtotime('-1 months'));
echo $prevmonth;


推荐答案

strtotime() code>使用30天,二月(今年)只有28天,所以不会在二月份生效。您可以使用当前日期 d j ,并减去将始终将您放在上个月( -29天):

strtotime() uses 30 day months and there are only 28 in days in February (this year) so will not yield a valid date in February. You could use the current day d or j and subtract that which will always put you in the previous month (-29 days):

$prevmonth = date('M', strtotime('-' . date('d') . ' days'));

这将获得 12月 January

这篇关于PHP日期返回错误月份减去一个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆