0

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"));
Qirel
  • 25,449
  • 7
  • 45
  • 62
Andy
  • 45
  • 7
  • 4
    It's not going to return false. Add another check to see if the month of the returned date is the same as the month you're looking for. – aynber Sep 13 '17 at 13:43
  • 4
    I mean, that is the 5th monday from the first monday of september... seems even better than returning false :) You could make your fn check if the month is the same... – Tyler Nichols Sep 13 '17 at 13:44
  • Possible duplicate of https://stackoverflow.com/questions/4150435/php-strtotime-last-monday-if-today-is-monday?rq=1 -- has the same idea. – Tyler Nichols Sep 13 '17 at 13:47
  • If you do `strtotime("foobar")` it returns false. `fifth monday september 2017` is the 5th Monday, counting from the 1st Monday of September. So it's pushed one month forward, but its not an invalid result for `strtotime()` – Qirel Sep 13 '17 at 13:47
  • 3
    I'd generally like to point out that `strtotime` isn't as magical as you may think. It understands *some* "freeform" descriptions of dates, but it's not omnipotent. Stop expecting too much from it. – deceze Sep 13 '17 at 13:56
  • Thank you all for the info, I now understand the function better :D – Andy Sep 13 '17 at 21:22

1 Answers1

2

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

Community
  • 1
  • 1
Matt S
  • 14,976
  • 6
  • 57
  • 76