Today I encountered a strange bug/feature in PhP-7 reported by a colleague in an application I wrote. It boils down to the following.
Consider the following:
echo date('Y-m-d', strtotime('first thu of Jan 2020'));
echo '<br/>';
echo date('Y-m-d', strtotime('first thu Jan 2020'));
echo '<br/>';
echo date('Y-m-d', strtotime('first wed of Jan 2020'));
echo '<br/>';
echo date('Y-m-d', strtotime('first wed Jan 2020'));
When I run it, I see the following:
2020-01-02
2020-01-02
2020-01-01
2020-01-08
Strangely 3rd and 4th lines of the output are different. Why? Is it a bug or one must use of
in such cases?
This behaviour is explicity mentioned in documentation (Second Note group, row 4):
"ordinal dayname" does advance to another day. (Example "first wednesday july 23rd, 2008" means "2008-07-30").
And see also the first Note:
Relative statements are always processed after non-relative statements. This makes "+1 week july 2008" and "july 2008 +1 week" equivalent.
So if you don't write the word "of
" the processor does not catch the statement "first day of". Consequently it split the string in two statements: first of all set the date to 1 Jan 2020 (through the statement Jan 2020
that it's interpreted as non-relative statement) then it applies the relative statement first wed
as mentioned above.
According to the documentation the format you have to use is
ordinal space dayname space 'of'
So the 'of'
is not optional. I cannot tell, why it is working in some cases without, but it looks like a coincidence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With