how to find the last monday of the month

前端 未结 3 541
死守一世寂寞
死守一世寂寞 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:39

    For everything date-related that you can express in proper English but have a hard time expressing using numbers, strtotime is your best friend.

    echo strtotime("last Monday of June 2011");
    

    This returns a timestamp that you can use as the second parameter to date and the likes to get a proper, human-readable date. Since it's a built-in function written in C, this solution is also much faster than almost anything else you could come up with written in PHP (though I'm quite sure it wouldn't matter much in a real-world scenario).

    So assuming you have $month=4, $dow=3 and $year=2011, you'll need an array mapping $month values to their English textual representations and another array mapping $dow values to their textual representations.

提交回复
热议问题