Reputation: 1194
<?php
$endDate = date("Y-m-d", strtotime("fourth monday april ". date("Y", strtotime("2013-04-01"))));
echo $endDate
?>
Why does this ouput 2013-04-29 when the correct answer is 2013-04-22?
Anytime the day you are searching for happens to fall on the first of a month, the day counter (i.e. "fourth") always displays one more than it should.
Example: "third wednesday may 2013" outputs 2013-05-22 when it should be 2013-05-15.
I am guessing the quick way to fix this would be to see if the first day of the month is the same day you are looking for and decrease the day counter ("fourth", "third" etc.) accordingly?
Upvotes: 1
Views: 2684
Reputation: 831
If other ones also looking for answer here for nth
day, for example 15. day of last month:
date("Y-m-d", strtotime(date("Y-m-d", strtotime("first day of previous month") ).' + 14 days') );
Upvotes: -1
Reputation: 20889
Change the phrasing to:
fouth monday of april ". date("Y", strtotime("2013-04-01"))
The key being of
.
Upvotes: 3