Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

php - strtotime('First Monday ..') when it's the first day of the month returns the wrong date

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

The above code returns 2012-10-08, which is incorrect - the first monday of october 2012 should return 2012-10-01.

The changelog from the manual (http://php.net/manual/en/function.strtotime.php) specifies the following:

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.

It seems to be clear that this is what's causing the wrong return date, however I'm using PHP version 5.4.7 (running xamp on localhost)!

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are missing the keyword of when trying to retrieve the first day of the month

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

I found the info on this bug report

My current version is PHP 5.4.4 in case it still does not work for you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...