简体   繁体   中英

PHP :How to get “ Nth Occurrence Of A Day Of The Week for a given month ”

How to get the what is the Occurrence Of A today Of The Week for a this month ??

Ex: this month starts from friday and so its(first Friday) and in this case first Sunday comes in 2nd week

I want to get what is the occurrence of today of this week for this month like today is

EX : "1st thursday of 2nd week "

Here's the php code I use. It may not be the best way, but at least it's an answer.

    $wkDay = date('w', mktime(0, 0, 0, $date->month, 1, $date->year));
    $weekCount = 1;
    $monthDay = $date->day;
    while ($monthDay > 7) {
        --$monthDay;
        ++$weekCount;
    }

My date class is structured to store day, month, and year values as numerical integers. The first line grabs the day of the week on the first day of the month I'm checking for. The week counter is initialized to one because I'm using it directly for output (eg echo "The ".$weekCount."th Tuesday of the month.";) Then I just take the day and increment my weeks each time I subtract seven.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM