以 PHP5 中的数组形式返回当前日历周的日期
我如何组合一个 PHP5 函数来查找当前日历周并以数组形式返回一周中每一天的日期(从星期一开始)?例如,如果该函数今天运行(2010 年 2 月 25 日星期四),该函数将返回一个数组,如下所示:
[0] => Mon Feb 22 2010<br />
[1] => Tue Feb 23 2010<br />
[2] => Wed Feb 24 2010<br />
[3] => Thu Feb 25 2010<br />
[4] => Fri Feb 26 2010<br />
[5] => Sat Feb 27 2010<br />
[6] => Sun Feb 28 2010<br />
日期以什么格式存储在数组中并不重要,因为我认为这很容易改变。另外,如果能够选择提供日期作为参数并获取该日期而不是当前日期的日历周,那就太好了。
谢谢!
How would I put together a PHP5 function that would find the current calendar week and return the dates of each day in the week as an array, starting on Monday? For example, if the function were run today (Thu Feb 25 2010), the function would return an array like:
[0] => Mon Feb 22 2010<br />
[1] => Tue Feb 23 2010<br />
[2] => Wed Feb 24 2010<br />
[3] => Thu Feb 25 2010<br />
[4] => Fri Feb 26 2010<br />
[5] => Sat Feb 27 2010<br />
[6] => Sun Feb 28 2010<br />
It doesn't matter what format the dates are stored as in the array, as I assume that'd be very easy to change. Also, it'd be nice to optionally be able to supply a date as a parameter and get the calendar week of that date instead of the current one.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想解决方案是首先使用
strtotime
< 获取与上周一相对应的时间戳/a>:但是,如果您尝试今天(星期四),使用类似以下内容:
您将得到:
即上周...对于 strtotime,“last” 表示 “今天之前的那个”。
这意味着您必须测试今天是否是 strtotime 返回的“上周一”加上一周 - 如果是,则添加一周...
这里是一个可能的解决方案(可能有更聪明的想法):
现在我们有了“上周一”的时间戳,我们可以编写一个简单的
for
循环,循环 7 次,每次添加 1 天,如下所示:给我们这样的输出:
现在,由您决定:
for
循环,以便将日期存储在数组中date
函数玩得开心;-)
I suppose a solution would be to start by getting the timestamp that correspond to last monday, using
strtotime
:But if you try with today (thursday), with something like this :
you'll get :
i.e. last week... For strtotime, "last" means "the one before today".
Which mean you'll have to test if today is "last monday" as returned by
strtotime
plus one week -- and, if so, add one week...Here's a possible (there are probably smarter ideas) solution :
And now that we have the timestamp of "last monday", we can write a simple
for
loop that loops 7 times, adding 1 day each time, like this :Which will give us this kind of output :
Now, up to you to :
for
loop so it stores the dates in an arraydate
functionHave fun ;-)
承租人。 。首先,为了避免任何时区问题,我会将您想要比较的那一天的中午固定下来,因此无论哪种方式一个小时都不会造成任何问题。
然后找到您约会的星期几。
1 是星期一,所以算出距离星期一还有多少天。
现在减去天数秒数,直到回到星期一。
现在整理一下你的日子。
这是一个时间戳数组,但如果您愿意,可以将日期存储为字符串。
Lessee. . First, to avoid any timezone issues I'd peg to noon on the day you want to compare against, so an hour either way won't cause any problems.
Then find the day of week of your date.
1 is Monday, so figure out how many days from Monday we are.
Now subtract days worth of seconds until you get back to Monday.
Now assemble your array of days.
This is an array of timestamps, but you could store the dates as strings if you prefer.