[英]How to find out next ISO 8601 week?
Lets say I have $year
and $week
variables that contain the ISO 8601 representation of a week. 可以说我有$year
和$week
变量,它们包含一个星期的ISO 8601表示形式。 I want to calculate the previous and the next weeks. 我想计算前几周和下几周。 This is an easy task in most parts of the year because I just have to increment or decrement the $week
variable, but in the border of two years I need to check if we started a new year. 在一年中的大部分时间里这是一件容易的事,因为我只需要增加或减少$week
变量,但是在两年的边界内,我需要检查是否开始新的一年。
I created something like this: 我创建了这样的东西:
public function nextweek($year, $week)
{
$maxweek = 0;//@TODO
if ($week + 1 == $maxweek)
{
$year++;
$week = 1;
}
else
{
$week++;
}
$nextweek = $year.'/'.$week;
return $nextweek;
}
Is there a completion, better solution or a built in function for this? 为此是否有完善,更好的解决方案或内置功能?
Finally I found a solution. 终于我找到了解决方案。
public function nextweek($year, $week)
{
$date = new DateTime;
$date->setISODate($year, ++$week);
if (!($date->format('W') == $week))
{
$week = '01';
++$year;
}
return $year.'/'.$week;
}
Or for PHP < 5.2 或对于PHP <5.2
function nextweek($year, $week) {
$nextWeek = strtotime($year.'W'.sprintf("%02d", $week)) + (7 * 24 * 60 * 60);
return date('Y/W', $nextWeek);
}
There is a problem with year wrap..for nextweek(2013, 52)
it prints 2013/01
. 年份换nextweek(2013, 52)
有问题。for nextweek(2013, 52)
2013,52 nextweek(2013, 52)
打印2013/01
。 Haven't found the error yet..so better stick with DateTime
. 还没有发现错误。所以最好坚持使用DateTime
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.