简体   繁体   English

php使用strtotime在一年的第一周之前获取正确的日期

[英]php get correct date by week one of the year using strtotime

I used this code to get the 1st date, of the 1st week, of the year: 我使用以下代码获取一年中第一周的第一天日期:

echo date('Y-m-d',strtotime('2018W01'));   #JAN 1 is Monday, returned 01/01
echo date('Y-m-d',strtotime('2013W01'));   #JAN 1 is Tuesday, returned 12/31
echo date('Y-m-d',strtotime('2014W01'));   #JAN 1 is Wednesday, returned 12/30
echo date('Y-m-d',strtotime('2015W01'));   #JAN 1 is Thursday, returned 12/29
echo date('Y-m-d',strtotime('2016W01'));   #JAN 1 is Friday, returned 01/04!? (shouldn't it be 12/28)
echo date('Y-m-d',strtotime('2022W01'));   #JAN 1 is Saturday, returned 01/03!? (shouldn't it be 12/27)
echo date('Y-m-d',strtotime('2017W01'));   #JAN 1 is Sunday, returned 01/02!? (shouldn't it be 12/26)

Since PHP, which is greater than 5.2.7, showing me that Monday is the 1st day of the week, then I was hoping that the year 2017, 1st day of week, should be 12/26. 由于PHP(大于5.2.7)向我显示星期一是一周的第一天,因此我希望2017年(一周的第一天)应该为12/26。 Is there a week number configuration somewhere for PHP to display 1st day, of the week, of the year correctly, for every year? PHP是否有某周的数字配置可以正确显示每年的每年的第一天? TIA TIA

echo date('Y-m-d',strtotime('2016W01'));
#JAN 1 is Friday, returned 01/04!? (shouldn't it be 12/28)

No, it shouldn't be 12/28, because first week of the year has this rules: 不,不应该是12/28,因为一年的第一周有以下规则:

  • It is the first week with a majority (4 or more) of its days in January. 这是第一周,一月的大部分时间(4天或以上)。
  • Its first day is the Monday nearest to 1 January. 它的第一天是距1月1日最近的星期一。
  • It has 4 January in it. 它有1月4日。 Hence the earliest possible dates are 29 December through 4 January, the latest 4 through 10 January. 因此,最早的日期可能是12月29日至1月4日,最近的是1月4日至10日。
  • It has the year's first working day in it, if Saturdays, Sundays and 1 January are not working days. 如果周六,周日和1月1日不是工作日,则它具有一年中的第一个工作日。

If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, it is part of week 53 of the previous year; 如果1月1日在星期一,星期二,星期三或星期四,则在第01周。如果1月1日在星期五,则是上一年的第53周; if on a Saturday, it is part of week 52 (or 53 if the previous year was a leap year); 如果在星期六,则是第52周的一部分(如果前一年是a年,则为53); if on a Sunday, it is part of week 52 of the previous year. 如果在星期日,则是上一年第52周的一部分。

Other examples are also correct. 其他示例也是正确的。 Read more here . 在这里阅读更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM