获取上周三午夜的 unix 时间戳
我如何找到上周三午夜的 unix 时间戳? 我唯一的方法是获取今天的日期索引和天数,并减去差值,但我可以想到几种可能失败的情况,例如,在星期三发生之前的一个月内。
我想,更简洁地说,如何找到上周三的日期?
任何帮助,将不胜感激。
How would I go about finding the unix timestamp of midnight of the previous Wednesday? My only approach would be to get the day index and day number of today, and subtract the difference, but I can think of several scenarios where this would fail, for example, early in a month before a Wednesday has happened.
I guess, more concisely, how do I find the date of the previous Wednesday?
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
strtotime
怎么样?你会得到这个输出:
这确实是上周三午夜。
What about
strtotime
?And you get this output :
which is indeed last wednesday, midnight.
这比你想象的要容易......观察这个完全运作的
战斗站的强大火力,呃,我的意思是strtotime这将输出
在撰写本文时,是上周三:)
It's easier than you might think...observe the awesome firepower of this fully operational
battle station, er, I mean strtotimeThis will output
Which, at the time of writing, is last wednesday :)
你想要有多“正确”? 如果您想要高度正确,则需要考虑夏令时、时区、闰年等。这意味着使用库而不仅仅是减去日期索引。
我目前没有使用 PHP,但我曾经在 Pear 库方面运气不错:
这是 Calendar 类的 docs
How "correct" do you want to be? If you want to be highly correct, you'll need to account for daylight savings time, time zones, leap years, etc. This means using a library and not just subtracting day indexes.
I am not currently doing PHP but I used to have good luck with the Pear libraries:
here are the docs for the Calendar class