how to find the last monday of the month

前端 未结 3 626
死守一世寂寞
死守一世寂寞 2021-01-05 18:55

I\'m not sure how to go about this one. I\'m building a calendar in PHP and need users to be able to add a repeating event that follows the following rule:

Last [DOW

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 19:33

    Here's an alternative:

     0)
                    return 29;
                # or if year is divisible by 400
                if ($year % 400 == 0)
                    return 29;
                return 28;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30; 
            default:
                return 31;
        }
    }
    
    function lastDayOfWeek($month, $year, $dow) {
        $d = new DateTime();
    
        #Finding the last day of month
        $d = $d->setDate($year, $month, lastDayOfMonth($month, $year));
    
        #Getting the day of week of last day of month
        $date_parts = getdate($d->getTimestamp());
    
        $diff = 0;
    
        #if we can't find the $dow in this week... (cause it would lie on next month)
        if ($dow > $date_parts['wday']) {
            # ...we go back a week.
            $diff -= 7;
        }
    
        return $date_parts['mday'] + $diff + ($dow - $date_parts['wday']);  
    }
    
    # checking the algorithm for this month...
    for ($i=0; $i < 7; $i++) {
        echo lastDayOfWeek(6,2011,$i) . "
    "; } ?>

提交回复
热议问题