22

The following line:

echo date('d', strtotime('First Saturday August 2015'));

prints 08, which doesn't seem to make any sense because the first occurrence of a day of the week can't be after the 7th.

Is it a php bug or a php bug or maybe even a php bug? I don't know...

php version: 5.5.19

1
  • 5
    Aug 1, 2015 is a Saturday, the first Saturday in August. Aug 8 is the second Saturday. Jan 29, 2015 at 15:33

3 Answers 3

45

Update #1: explained below the big difference that a simple word like "of" makes, after I investigated a little in the PHP source code.

Update #2: There actually is a documentation page on PHP manual that explains the formats of dates accepted by strftime(). I just was not aware of it until now. Thanks @Outspaced for providing the link in their answer.

For a quick and easy reading skip the section Update #1.


The initial answer:

Indeed,

echo date('Y-m-d', strtotime('First Saturday August 2015'));

prints:

2015-08-08

But

echo date('Y-m-d', strtotime('First Saturday of August 2015'));

prints the correct date:

2015-08-01

Since strtotime() tries to "understand" English, I guess you and it speak different dialects :-)

Update #1:

After some reading in the PHP source code of function strtotime(), I think that it parses First Saturday August 2015 as First Saturday followed by August 2015.

August 2015 is an absolute expression and because there is no indication about the day it produces 2015-08-01 (the 1st of August 2015) which is a reasonable guess (the only one possible, I think).

First Saturday is a relative expression. It is relative to an absolute mark provided in the string (August 2015) or as a second parameter of strtotime() and it "shifts" the timestamp of the absolute mark back or forward with some amount.

Putting them together, strtotime() interprets First Saturday August 2015 as First Saturday counting since August 1, 2015 and that is, indeed, August 8, 2015.

On the other hand, First Saturday of August 2015 is parsed using a different rule. One of next, last, previous, this or any ordinal number from first to twelfth, followed by a weekday name (full or abbreviated) and followed by of is parsed as a weekday of the provided month (or the current month if none is provided in the string).

Update #2

After I found out that the format of the string accepted by strtotime() is actually explained in the PHP documentation, the explanation became more simple. Take a look at the second "Note:" box in the documentation page.

First Saturday August 2015 is explained at item #4 in the note:

"ordinal dayname" does advance to another day.

First Saturday of August 2015 is explained at item #6 of the note:

"ordinal dayname 'of'" does not advance to another day.

The note ends with an entire block of explanation dedicated to the magical word "of".

14
  • It's interesting that echo date('Y-m-d', strtotime('Saturday August 2015')); also prints the correct date.
    – axiac
    Jan 29, 2015 at 15:39
  • 1
    I'm not a native speaker either and the part regarding the dialect was just a joke ;-) PHP doesn't understand English, that function follows a set of rules to parse the dates. There are lots of rules, maybe that's why they are not described in the documentation.
    – axiac
    Jan 29, 2015 at 16:02
  • 5
    This is why php's strtotime is both amazing and terrible at the same time
    – yuvi
    Jan 29, 2015 at 23:18
  • 1
    @BrianWarshaw what? No. The results are always consistent and reliable. It's very easy to make mistakes, but that's what you get for using PHP I guess
    – yuvi
    Jan 30, 2015 at 19:19
  • 3
    @BrianWarshaw (regarding "why incur the cost of translating from a string intermediary?") Because it's easier to write $date = strtotime('First Saturday of August 2015'); than $ts = mktime(0, 0, 0, 8, 1, 2015); $date = $ts + 86400 * (6 - date('w', $ts)); and furthermore, it's easier to read and understand :-) Let's recall that PHP started as a tool whose aim was to make the coding of web pages an easy task.
    – axiac
    Jan 31, 2015 at 17:21
12

You need an 'of':

date('d/m/Y', strtotime('First Saturday of August 2015'))

see the manual: http://docs.php.net/manual/en/datetime.formats.relative.php

0
-3

As stated, PHP didn't quite understand your choice of dialect. If you plan on accepting varied input from your users (not a bad idea) to express a date, I suggest doing some normalization of that user input yourself so that you can submit a predictable format to strtotime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.