2012-04-30 48 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