2013-02-06 37 views
2
echo date('Y-m-d', strtotime('First Monday '.date('F o', strtotime("-4 months"))); 

上面的代碼返回2012-10-08,這是不正確的 - 2012年10月的第一個星期一應該返回2012年-10-01。strtotime('First Monday ..')當它是每月的第一天返回錯誤的日期

從手動(http://php.net/manual/en/function.strtotime.php)更改日誌指定以下內容:

In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a 
month where that weekday was the first day of the month would incorrectly add 
one week to the returned timestamp. 

This has been corrected in 5.2.7 and later versions. 

這似乎是清楚的,這是什麼導致了錯誤的返回日期,但是我使用PHP版本5.4.7(運行本地主機上的xamp)!

任何想法?

回答

5

你缺少關鍵字of嘗試檢索

echo date('Y-m-d', strtotime('First Monday of '.date('F o', strtotime("-4 months"))); 

我發現這個bug report

我的當前版本的信息每個月的第一天是PHP 5.4.4的情況下,仍不適合你

+0

謝謝!這工作完美! –

+0

@ user1029502:沒問題,別忘了接受答案 – koopajah

+1

PS!格式「2012年10月第一個星期一」在PHP 5.2.7之前不適用於PHP。因此,如果您收到包含1970年的任何返回日期,則需要在strtotime()函數中省略「of」。 請記住,「第一個」在5.2.7之前的PHP版本上無法正常工作, –

相關問題