当周跨越两年时,从 PHP date() 获取一年中的年份和周
我在使用 PHP 的 date()
函数时遇到了一个有趣的问题。还没有找到关于这个问题的讨论或使用谷歌,但也许其他人以前遇到过同样的问题?
我正在尝试获取给定时间戳的年份和年份。这是我正在使用的代码:
date("Y-\WW");
截至今天,它正确输出:2011-W39
。
问题是在提供时间戳时,如果有问题的时间戳是 2011 年 1 月 3 日(实际上是“2010 年第 52 周”的一部分)。PHP 正确返回 W52,但它也返回 2011 年作为年份,而不是 2010。
date("Y-\WW", 1294016400);
输出: 2011-W52
有关如何解决此问题的任何想法吗?我应该注意,尽管在这种情况下很容易比较输出的 strtotime()
大于当前的 time() 并进行调整,但我需要一个也适用于前几年的解决方案(例如,如果 2010 年 1 月 3 日发生了同样的事情)。
I have run into an interesting issue using PHP's date()
function. Haven't had any luck locating a discussion of this on SO or using Google, but maybe someone else has run into the same issue before?
I am trying to get the YEAR and the WEEK OF THE YEAR for a given timestamp. This is the code I am using:
date("Y-\WW");
Which as of today correctly outputs: 2011-W39
.
The problem is when supplying a timestamp, if the timestamp in question is, for example, on January 3rd, 2011 (which was actually part of the "52nd week of 2010". PHP correctly returns W52, but it also returns 2011 as the year, instead of 2010.
date("Y-\WW", 1294016400);
Outputs: 2011-W52
Any idea on how to solve this problem? I should note that although in this case it would be easy to just compare that the strtotime()
of the output is greater than the current time()
and adjust, but I need a solution that will work for previous years as well (e.g. if the same thing happened for January 3rd 2010).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
天哪,显然我没有足够仔细地重新阅读文档,在 PHP 5.1.0 中添加了字母
o
:来自
日期()
:所以我的代码应该是
date("o-\WW");
Darn, apparently I didn't re-read the documentation closely enough, the letter
o
was added in PHP 5.1.0:From the documentation for
date()
:So my code should have been
date("o-\WW");