2013-02-06 39 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版本上无法正常工作, –

相关问题