这在昨天没有改变代码的情况下工作得很好。
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());它昨天产生的输出正如您所预期的那样-例如,4月,5月,6月,7月
今天,它呼应了五月五月七月。
有什么想法吗?
提前谢谢。
发布于 2009-07-31 10:38:23
这可能与bug #44073有关
您可以尝试如下所示:
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";(在的评论部分找到解决方案;)
和输出:
Apr
May
Jun
Jul在日期格式和月份名称等方面有点“作弊”...
发布于 2012-06-01 00:24:38
Gorden正确地指出了这个问题,但我想给出另一个有用的解决方案,而不是技术上的。只需在strtotime中使用"first day of“或"last day of”即可。例如,以下示例在一个月的31号解决了这个问题:
// Today is May 31st
//All the following return 2012-04-30
echo date('Y-m-d', strtotime("last day of -1 month"));
echo date('Y-m-d', strtotime("last day of last month"));
echo date_create("last day of -1 month")->format('Y-m-d');
// All the following return 2012-04-01
echo date('Y-m-d', strtotime("first day of -1 month"));
echo date('Y-m-d', strtotime("first day of last month"));
echo date_create("first day of -1 month")->format('Y-m-d');发布于 2009-07-31 10:40:42
尝试使用此选项,而不是strtotime
mktime(0, (date("n") - 3 + 12) % 12, 1)这个想法是把当前的月份数字(date("n"))减去你想要的月数(这里是-3),再加上12,然后得到模12。
https://stackoverflow.com/questions/1211824
复制相似问题