Another way is to find which day of the week today, find the first such day of the month through strtotime() magic, and then calculate the difference between this and now in weeks. See below a function that will accept Ymd formatted date() and return that day of the week of the month.
Note: strtotime must be verbose, including the βfromβ and the month: βfirst Monday 2011-02β, otherwise βitβs moving forward one day . This is a bit of me when I tested edge cases.
Also added some display pepper, which is completely optional, but I liked it.
function nthWeekdayOfMonth($day) { $dayTS = strtotime($day) ; $dayOfWeekToday = date('l', $dayTS) ; $firstOfMonth = date('Y-m', $dayTS) . "-01" ; $firstOfMonthTS = strtotime($firstOfMonth) ; $firstWhat = date('Ym-d', strtotime("first $dayOfWeekToday of $monthYear", $firstOfMonthTS)) ; $firstWhatTS = strtotime($firstWhat) ; $diffTS = $dayTS - $firstWhatTS ; $diffWeeks = $diffTS / (86400 * 7); $nthWeekdayOfMonth = $diffWeeks + 1; return $nthWeekdayOfMonth ; } $day = date('Ym-d') ; $nthWeekdayOfMonth = nthWeekdayOfMonth($day) ; switch ($nthWeekdayOfMonth) { case 1: $inflector = "st" ; break ; case 2: $inflector = "nd" ; break ; case 3: $inflector = "rd" ; break ; default: $inflector = "th" ; } $dayTS = strtotime($day) ; $monthName = date('F', $dayTS) ; $dayOfWeekToday = date('l', $dayTS) ; echo "Today is the {$nthWeekdayOfMonth}$inflector $dayOfWeekToday in $monthName" ;
Fanis source share