PHP日期:2015/2016的周数
As stated in ISO 8601 there are years that will have 53 weeks, 2015 is one of those. What will be the next week number in 2016? Does it start with week 01 again? The reason I am asking is that in one of my projects an URL "2016-W01" does show the wrong days (eg. Mon. 2nd Jan 2016 - where it should be Mon. 4th Jan 2016). If I am calling "2016-W02" the correct days are coming up (4th-10th Jan.), but "obviously?" this should be week01 and not week2..
Any ideas on this? Thanks!
如ISO 8601所述,有些年将有53周,2015年就是其中之一。 2016年的下周会是什么? 它是否从第01周开始? 我问的原因是,在我的一个项目中,URL“2016-W01”确实显示错误的日期(例如,2016年1月2日星期一 - 应该是2016年1月4日星期一)。 如果我打电话给“2016-W02”,正确的日子即将到来(1月4日至10日),但“很明显?” 这应该是第01周而不是第2周.. p>
有关于此的任何想法吗? 谢谢! p> div>
<?php
$dt = new DateTime('2015-12-27');
for($i=0; $i<12; $i++) {
echo $dt->format('Y-m-d , D, W'), "
";
$dt->modify('+ 1 day');
}
prints (without the descriptions)
2015-12-27 , Sun, 52
2015-12-28 , Mon, 53\
2015-12-29 , Tue, 53|
2015-12-30 , Wed, 53|
2015-12-31 , Thu, 53 <-- this thursday is in 2015, therefore this is week 53
2016-01-01 , Fri, 53|
2016-01-02 , Sat, 53|
2016-01-03 , Sun, 53/
2016-01-04 , Mon, 01\
2016-01-05 , Tue, 01|
2016-01-06 , Wed, 01|
2016-01-07 , Thu, 01 <-- this is the first thursday in 2016, therefore this is week 1
edit and btw: You might want to switch your project to use the standard php date/time parser:
<?php
$dt = new DateTime('2016W01'); echo $dt->format('Y-m-d, D, W'), "
";
$dt = new DateTime('2016W02'); echo $dt->format('Y-m-d, D, W'), "
";
prints
2016-01-04, Mon, 01
2016-01-11, Mon, 02
The Week starts from 01 on 1st January only. But in this case 1st January is in week 53 so it will skip the week 01 and show you as week 02 for 4-10 JAN.