xpoes
xpoes

Reputation: 61

Calculating third sunday of a month

I use this code to determine which month we're in:

$monthyear = date('F Y');

and then this to determine the third Sunday of a month

$thirdsunday = date("D, j M Y", strtotime($monthyear . ' third sunday'));

The outcome is Sun, 22 Mar 2015, which happens to be the 4th Sunday of the month (Sun 1 March, 8 March, 15 March)..

What is happening here?

Upvotes: 1

Views: 1094

Answers (2)

chiliNUT
chiliNUT

Reputation: 19592

"ordinal dayname" does advance to another day. (Example "first wednesday july 23rd, 2008" means "2008-07-30").

http://php.net/manual/en/datetime.formats.relative.php

This means that, when you say third sunday without the of, rather than doing "the third sunday of the month", it does, the third sunday after the given date; when you specify only a month, the given date is interpreted to be the first of the month, so the 3rd sunday after the first--when the first is a sunday-- would thus be the fourth sunday of the month.

Upvotes: 1

Kevin
Kevin

Reputation: 41903

It should be the third sunday of March 2015

$thirdsunday = date("D, j M Y", strtotime('third sunday of ' . $monthyear));

http://php.net/manual/en/datetime.formats.relative.php

Upvotes: 2

Related Questions