php使用strtotime在一年中的第一周获得正确的日期 [英] php get correct date by week one of the year using strtotime

查看:37
本文介绍了php使用strtotime在一年中的第一周获得正确的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码来获取一年中第 1 周的第 1 个日期:

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)

由于 PHP 大于 5.2.7,显示星期一是一周的第一天,所以我希望 2017 年,一周的第一天,应该是 12/26.是否有周数配置可供 PHP 正确显示每年的第一天、一周的第一天、一年的第一天?TIA

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. Is there a week number configuration somewhere for PHP to display 1st day, of the week, of the year correctly, for every year? TIA

推荐答案

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

不,它不应该是 12/28,因为一年的第一周有以下规则:

No, it shouldn't be 12/28, because first week of the year has this rules:

  • 第一周大部分(4 天或更多)天都在 1 月.
  • 第一天是离 1 月 1 日最近的星期一.
  • 其中包含 1 月 4 日.因此,最早的可能日期是 12 月 29 日至 1 月 4 日,最晚的日期是 1 月 4 日至 10 日.
  • 如果星期六、星期日和 1 月 1 日不是工作日,则它包含当年的第一个工作日.

如果 1 月 1 日是星期一、星期二、星期三或星期四,则在第 01 周.如果 1 月 1 日是星期五,则是上一年第 53 周的一部分;如果在星期六,则是第 52 周的一部分(如果前一年是闰年,则是第 53 周);如果是星期日,则是上一年第 52 周的一部分.

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; if on a Saturday, it is part of week 52 (or 53 if the previous year was a leap year); if on a Sunday, it is part of week 52 of the previous year.

其他例子也是正确的.在此处阅读更多信息.

Other examples are also correct. Read more here.

这篇关于php使用strtotime在一年中的第一周获得正确的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆