Reputation: 18427
strtotime
is kinda magical. And while I know it can't do anything, I always guess things before looking for the correct solution.
So I wanted to get the first Sunday and Monday of next month. I tried this:
strtotime("first Sunday next month")
strtotime("first Monday next month")
after formatting (%d/%m/%y
) the results are:
12/11/2014
13/11/2014
Those are a Wednesday and a Thursday. What gives? I expected it won't work or give me the wrong Sunday, but what's a Wednesday doing there?
update
I don't need help figuring out how to get the next Sunday. I know how to do the correct calculations (I know how to look up the proper way anyway). My guess above was just me messing around looking for a shortcut. I am wondering, however why I'm getting this specific weird result which seems completely unrelated to everything I wrote
Upvotes: 0
Views: 544
Reputation: 41050
Try
// strtotime("first Sunday", strtotime("next month")); // wrong
echo date("%d/%m/%y", strtotime("first sunday of next month"));
But outputs invalid results for PHP 4.3 - 5.2.17!
Upvotes: 2
Reputation: 146460
Try this:
echo date('r', strtotime("first Sunday of next month")) . PHP_EOL;
echo date('r', strtotime("first Monday of next month")) . PHP_EOL;
It's very tricky but you need to make sure that your format is exactly described in the Relative Formats section of the manual. In this case you want ordinal space dayname space 'of'. Otherwise, it's probably just doing two different calculations in sequence.
In this case, since the whole expression doesn't have a known format it's getting split:
Test code:
echo date('r', strtotime("first Sunday next month")) . PHP_EOL;
echo date('r', strtotime("first Monday next month")) . PHP_EOL;
echo PHP_EOL;
echo date('r', strtotime("first Sunday")) . PHP_EOL;
echo date('r', strtotime("first Monday")) . PHP_EOL;
echo date('r', strtotime("next month")) . PHP_EOL;
Disclaimer: I almost never get it right myself.
Upvotes: 3