本文介绍了如何获得年度敏感的日历周?日期(“W”)在1月1日返回52的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所示,PHP的日期(W)功能可以恢复日历周(当天)。不幸的是,大部分年份的第一天都可以退回52或53。这是一个简单的思维方式,正确但非常烦人,因为2012年1月1日不是第52周,它不是今年的一个日历周。大多数日历将其定义为上一周的第0周或第52周。



当您将一年中的每一天的日历周分组时,这是非常棘手的:1月1日2012年12月31日和2012年12月31日都被放在同一个日历周组。



所以我的问题是:是否有(本地) PHP的日期(W)?



编辑:我想我以非常不清楚的方式写了这个问题的第一个版本,所以这是我的编辑:我正在寻找一个功能,给一年的第一天的正确的日历周。 PHP的日期(W)在2012年1月1日返回52,这是错误的。它应该为0或为空。据官方消息,一年的第一个日历周从一年的第一个星期一开始。所以,如果一年的第一天不是星期一,那不是第1周!这是第0周。说



如果1月1日在星期一,星期二,星期三或星期四,则是在01周。如果1月1日星期五,星期六或星期日在星期五的52周或53周



这一切变得棘手,因为一年的最后几天也是在52/53周。日期(W)不分为当年和上年。

解决方案

53和一周之前1周至0周的一切。

  $ w =(int)date('W'); 
$ m =(int)date('n');
$ w = $ w == 1?($ m == 12?53:1):( $ w> = 51?($ m == 1?0:$ w):$ w)

echoweek $ w in.date('Y');

2013-12-31 ==> 2013年第53周



2014-01-01 ==> 2014年的第1周



2015-12-31 ==> 2015年第52周



2016-01-01 ==> 2016年第0周



一个小的测试运行,所以你可以看到自己; - )

  $ id = array(25,26,27,28,29,30,31,1,2,3 ,4,5,6,7,8); ($ iy = 2013; $ iy< 2067; ++ $ iy){foreach($ id as $ k => $ v){if($ k< 7){$ im = 12;} else {$ im = 1;} 
if($ k == 7){++ $ iy; echo'====< br>';} $ tme = strtotime($ im / $ v / $ iy);
echo date('d-m-Y',$ tme),'* *';
//实际代码=================
$ w =(int)date('W',$ tme);
$ m =(int)date('n',$ tme);
$ w = $ w == 1?($ m == 12?53:1):( $ w> = 51?($ m == 1?0:$ w):$ w)
//实际代码=================
echo'< b> WEEK:',$ w,'---',' YEAR:',date('Y',$ tme),'< / b>< br>';} - $ iy;
echo'----------------------------------< br>';}


As the headline says, PHP's date("W") function gives back the calendar week (for the current day). Unfortunatly it gives back 52 or 53 for the first day(s) of most years. This is, in a simple thinking way, correct, but very annoying, as January 1st of 2012 is not calendar week 52, it's NOT a calendar week of the current year. Most calendars define this as week 0 or week 52 of the previous year.

This is very tricky when you group each day of the year by their calendar week: 1st of January 2012 and 31st of December 2012 are both put into the same calendar week group.

So my question is: Is there a (native) year-sensitive alternative to PHP's date("W") ?

EDIT: I think I wrote the first version of this question in a very unclear way, so this is my edit: I'm searching for a function that gives back the correct calendar week for the first day(s) of the year. PHP's date("W") gives back 52 for the 1st of January 2012, which is "wrong". It should be 0 or null. According to official sources, the first calendar week of a year starts on the first monday of the year. So, if the first day of a year is not a monday, it's not week 1 ! It's week 0. The wikipedia article says

If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year.

This becomes tricky as the last days of the year are also in week 52/53. date("W") does not divide into current year and previous year.

解决方案

This solution converts the excess of december to week 53 and everything in january prior to week 1 to week 0.

$w=(int)date('W');
$m=(int)date('n');
$w=$w==1?($m==12?53:1):($w>=51?($m==1?0:$w):$w);

echo "week $w in ".date('Y');

2013-12-31 ==> week 53 in 2013

2014-01-01 ==> week 1 in 2014

2015-12-31 ==> week 52 in 2015

2016-01-01 ==> week 0 in 2016

And a small test run, so you can see for yourself ;-)

$id=array(25,26,27,28,29,30,31,1,2,3,4,5,6,7,8);
for($iy=2013;$iy<2067;++$iy){foreach($id as $k=>$v){if($k<7){$im=12;}else{$im=1;}
if($k==7){++$iy;echo '====<br>';}$tme=strtotime("$im/$v/$iy");
echo date('d-m-Y',$tme),'  * *  ';
//THE ACTUAL CODE =================
    $w=(int)date('W',$tme);
    $m=(int)date('n',$tme);
    $w=$w==1?($m==12?53:1):($w>=51?($m==1?0:$w):$w);
//THE ACTUAL CODE =================
echo '<b>WEEK: ',$w,' --- ','YEAR: ',date('Y',$tme),'</b><br>';}--$iy;
echo '----------------------------------<br>';}

这篇关于如何获得年度敏感的日历周?日期(“W”)在1月1日返回52的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 09:15