2012-04-30 51 views
2

这真是有线 作为30-APR-12PHP的strtotime ERROR

两个:

date("M", strtotime("-2 months")); 
date("M", strtotime("-1 months")); 

给我同样的结果,如: 三月 月

其他任何一天,它是好。我太新了,无法告诉我该怎么办?

我已经XAMPP 1.7.4包(PHP 5.3.5)

+0

发现解决方案[Solution_link](http://stackoverflow.com/questions/1211824/php-strtotime-1month-2month)这里。 使用日期(“M”,strtotime(“ - 2 month”,strtotime(date(“F”)。“1”)))改为...不知道为什么? 如果有人解释会很棒......谢谢! – farness

回答

1

两个日期你实际上又回到是:

  • 2012-03-01
  • 2012-03- 30

对于像这样的月份,strtotime()的行为有点棘手。如果你回去两个月,那么二月三十号还没有,所以你在三月一号结束。返回1个月是你期望的行为。

要解决此问题,请在使用strtotime()时使用月份部分。

date('M', strtotime('-2 months', strtotime('2012-04-01'))); 

只是当前一个替换年/月。

+0

谢谢...我使用日期(“M”,strtotime(“ - $我月”,strtotime(日期(“​​F”)。“1”)))而是 – farness