问题描述
我使用PHP的 date()
函数遇到一个有趣的问题。没有任何运气找到在这个SO或使用Google的讨论,但也许有人遇到同样的问题?
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?
我正在尝试获得YEAR和YEAR年份给定时间戳。这是我使用的代码:
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);
截至今日正确输出: 2011-W39
。
问题是提供时间戳记,如果有问题的时间戳是例如2011年1月3日(实际上是2010年第52周的一部分),PHP正确地返回W52,但也返回2011年年,而不是2010年。
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);
输出: 2011-W52
解决这个问题?我应该注意,虽然在这种情况下,很容易比较,输出的 strtotime()
大于当前 time()
并进行调整,但是我需要一个前几年的解决方案(例如,如果2010年1月3日发生同样的事情)。
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).
推荐答案
Darn,显然我没有重新阅读文档c在PHP 5.1.0中添加了信件 o
Darn, apparently I didn't re-read the documentation closely enough, the letter o
was added in PHP 5.1.0:
从:
所以我的代码应该是 date(o-\ WW);
这篇关于从PHP日期()获取年份和周一周,每周跨度两年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!