Andy
Andy

Reputation: 45

strtotime("fifth monday september 2017") returns "2nd October, 2017"?

Does anyone know how to get this to return false?

There is no 5th Monday in Sept, so this function returns: 2nd October, 2017

date("jS F, Y", strtotime("fifth monday september 2017"));

Upvotes: 0

Views: 180

Answers (1)

Matt S
Matt S

Reputation: 15394

PHP's strtotime is based on GNU's date parsing C function (IIRC it actually calls this C function instead of using its own implementation). From the GNU documentation:

The explicit mention of a day of the week will forward the date (only if necessary) to reach that day of the week in the future... A number may precede a day of the week item to move forward supplementary weeks.

Therefore you will not get a false result directly from strtotime with the example you provide. If you want to confirm the result is in the same month, you can do something like:

$timestamp = strtotime("fifth monday september 2017");
$month = date('n', strtotime("september 1, 2017"));
$same_month = (date('n', $timestamp) === $month);

Also from the GNU documentation, to add some perspective:

Our units of temporal measurement, from seconds on up to months, are so complicated, asymmetrical and disjunctive so as to make coherent mental reckoning in time all but impossible... Unlike the more successful patterns of language and science, which enable us to face experience boldly or at least level-headedly, our system of temporal calculation silently and persistently encourages our terror of time... It is no wonder then that we often look into our own immediate past or future, last Tuesday or a week from Sunday, with feelings of helpless confusion.

-Robert Grudin, Time and the Art of Living

Upvotes: 2

Related Questions