# Need to find out if it's the second monday in the month, fourth tuesday, etc.

• 02-26-2014, 09:54 AM
chrisrozwod
Need to find out if it's the second monday in the month, fourth tuesday, etc.
Hi,

So basically, I need to find out which day of the week it is, and then how many times that weekday has appeared this month (is it the first thursday, second monday, etc.).

First, I'll get a date:

PHP Code:

``` \$getdate = date("Y\-m\-d");  ```
Next I need to find out what day of the week it is:

PHP Code:

``` \$date_day = date('N', strtotime(\$getdate));  ```
Next, and the part I'm struggling with, I need to find out which instance this day is in the month. Is it the first thursday? Second monday?

I thought I'd found something that worked, but it just gave me what week it was, which is different. Like for today, it would give me the fifth week, but it's only the fourth wednesday.

I'd really appreciate some help here. I keep finding ways to find out which week it is, but I can't find anything on which which instance a specific day of the week it is.
• 02-26-2014, 04:12 PM
Keleth
As far as I can see, this is a multistep process, as the logic is a bit more complex.

First, you need to know how many days in the current month:

PHP Code:

``` \$daysInMonth = date('t');  ```
Then you need to figure out what day of the week the first day of the month is:

PHP Code:

``` \$firstDay = date('N', strtotime(date('Y-m-1')));  ```
And with that, you can figure out how many days of a particular type, by shifting by the offset of the first day:

PHP Code:

``` \$dayCount = \$daysInMonth / 7; \$remainder = \$daysInMonth % 7; if (\$remainder >= date('N', strtotime(\$getdate)) - \$firstDay) \$dayCount += 1;  ```
Of course, there may be an easier way to do this, and I didn't run this code, so corrections are probably necessary. But the logic should be sound, I've done it before but it was a while ago.
• 02-27-2014, 09:40 AM
chrisrozwod
Thanks for the help. I tried it out, but it seemed to just tell me how many times that day occurs. I was looking for something that could tell me not the total, but what number we're currently on (or whatever day we're examining is on). However, part of your code gave me an idea for a simple tweak to what I had before and I think I have it working now.

The first day bit....I don't know why that didn't occur to me earlier, but it didn't. Anyway, remember I was saying earlier how I had a thing that would tell me which instance with regard to weeks? Well, I thought if I combined that with checking if the date day was less than the first day, then I could just subtract one from the week thing. It seems to work.

The whole thing, if you're interested:
PHP Code:

``` function getWeeks(\$date){    \$cut = substr(\$date, 0, 8);    \$daylen = 86400;    \$timestamp = strtotime(\$date);    \$first = strtotime(\$cut . "00");    \$elapsed = (\$timestamp - \$first) / \$daylen;    \$i = 1;    \$weeks = 1;    for(\$i; \$i<=\$elapsed; \$i++)    {        \$dayfind = \$cut . (strlen(\$i) < 2 ? '0' . \$i : \$i);        \$daytimestamp = strtotime(\$dayfind);        \$day = strtolower(date("l", \$daytimestamp));        if(\$day == strtolower("monday"))  \$weeks ++;    }    return \$weeks;}\$getdate = date("Y\-m\-d");\$first_day = date('N', strtotime(date('Y-m-1', strtotime(\$getdate)))); \$date_day = date('N', strtotime(\$getdate));\$date_week = getWeeks(\$getdate);if (\$date_day < \$first_day) {    \$date_week = \$date_week - 1;}  ```
• 02-27-2014, 03:38 PM
Keleth
Ah, I misunderstood your request. Yah, that works.
• 02-27-2014, 03:47 PM
Fou-Lu
Perhaps I'm just not understanding what you are looking for, but this sounds to be as simple as:
PHP Code:

``` function weekOfMonth(DateTime \$dt) {     static \$aNth = array("first", "second", "third", "fourth", "fifth");     return \$aNth[ceil(\$dt->format('d') / 7) - 1]; }  ```
That should tell you which week of the month it is with the string result. For a quick test, you can run with this:
PHP Code:

``` \$dtStart = new DateTime(); \$dtEnd = clone \$dtStart; \$dtEnd->modify('+1 month'); \$diInterval = new DateInterval('P1D'); \$itor = new DatePeriod(\$dtStart, \$diInterval, \$dtEnd); foreach (\$itor AS \$dt) {     printf('%s is the %s %s of %s' . PHP_EOL,         \$dt->format('F j Y'),         weekOfMonth(\$dt),         \$dt->format('l'),         \$dt->format('F')); }  ```
And you can put whatever start date into the \$dtStart to verify.