Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...