如何找到一个月的最后一个星期一 [英] how to find the last monday of the month
问题描述
我不确定该怎么做.我正在用PHP构建日历,需要用户能够添加遵循以下规则的重复事件:
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:
每月的最后[DOW](所以一周的最后[Mon/Tues/Wed/etc]等)
Last [DOW] of the month (so Last [Mon/Tues/Wed/etc] of the week)
我已经存储了规则本身,只是不确定如何最好地推断PHP的本月的最后一个Mon/Tue/Wed?我跌倒了,好像让事情变得复杂了.
I've got the rule itself stored, I'm just not sure how best to extrapolate the last Mon/Tue/Wed of the month in PHP? I fell like i'm making this more complicated than it needs to be.
假设您有$month=4
,$dow=3
和$year=2011
的变量,我该怎么做呢?
Assuming you have variables for $month=4
, $dow=3
and $year=2011
how would I best do this?
推荐答案
对于所有与日期相关的内容,您都可以用适当的英语来表达,但很难用数字来表达,
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");
这将返回一个时间戳,您可以将其用作 date
的第二个参数之类的方法来获取正确的,易于理解的日期.由于它是用C编写的内置函数,因此该解决方案也比用PHP编写的几乎任何其他东西都要快得多(尽管我很确定在实际情况下这无关紧要).
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).
因此,假设您具有$month=4
,$dow=3
和$year=2011
,则需要一个将$month
值映射到其英文文本表示形式的数组,以及另一个将$dow
值映射到其文本表示形式的数组.
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.
这篇关于如何找到一个月的最后一个星期一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!