[英]PHP - get last week number in year
请告诉我如何获得一年中的最后几周?
function getIsoWeeksInYear($year) {
$date = new DateTime;
$date->setISODate($year, 53);
return ($date->format("W") === "53" ? 53 : 52);
}
The o
date format gives the ISO-8601 year number . o
日期格式给出了ISO-8601 年份编号。 We can use this, and the fact that "invalid" dates are automatically rolled around to make valid ones ( 2011-02-31
gives 2011-03-03
), to determine if a given year has 53 weeks.我们可以使用它,以及“无效”日期自动滚动以生成有效日期( 2011-02-31
给出2011-03-03
)的事实,以确定给定年份是否有 53 周。 If does not, then it must have 52.如果没有,那么它必须有 52。
See also, date format characters and DateTime::setISODate()
manual pages.另请参阅日期格式字符和DateTime::setISODate()
手册页。
In ISO-8601 specification, it says that December 28th is always in the last week of its year .在ISO-8601规范中,它说12 月 28 日总是在其一年的最后一周。
Based on that, we can simply create that date, and see in what week it is:基于此,我们可以简单地创建该日期,并查看它是哪一周:
$dt = new DateTime('December 28th');
echo $dt->format('W'); # 52
$dt = new DateTime('December 28th, 2009');
echo $dt->format('W'); # 53
... or if you are using date()
and strtotime()
functions: echo date('W', strtotime('December 28th')); # 52
...或者如果您使用date()
和strtotime()
函数: echo date('W', strtotime('December 28th')); # 52
echo date('W', strtotime('December 28th')); # 52
Replace 2010 by the year you want and Europe/Berlin with your timezone:将 2010 替换为您想要的年份,将 Europe/Berlin 替换为您的时区:
<?php
date_default_timezone_set('Europe/Berlin');
echo gmdate("W", strtotime("31 December 2010"));
?>
You'll get one of the values 01
, 52
or 53
.您将获得值01
、 52
或53
。
Just for fun ( Demo ):只是为了好玩(演示):
<?php
date_default_timezone_set('Europe/Berlin');
echo "ISO-8601 week number of year, weeks starting on Monday, of 31 December:\n\n";
for ($year = 1900; $year < 2100; $year++) {
$date = strtotime("31 December $year");
echo "$year => ", gmdate("W", $date), "\n";
}
?>
Output:输出:
ISO-8601 week number of year, weeks starting on Monday, of 31 December:
1900 => 01
1901 => 01
1902 => 01
1903 => 53
1904 => 52
1905 => 52
1906 => 52
1907 => 01
1908 => 53
1909 => 52
1910 => 52
1911 => 52
1912 => 01
1913 => 01
1914 => 53
1915 => 52
1916 => 52
1917 => 52
1918 => 01
1919 => 01
1920 => 53
1921 => 52
1922 => 52
1923 => 52
1924 => 01
1925 => 53
1926 => 52
1927 => 52
1928 => 52
1929 => 01
1930 => 01
1931 => 53
1932 => 52
1933 => 52
1934 => 52
1935 => 01
1936 => 53
1937 => 52
1938 => 52
1939 => 52
1940 => 01
1941 => 01
1942 => 53
1943 => 52
1944 => 52
1945 => 52
1946 => 01
1947 => 01
1948 => 53
1949 => 52
1950 => 52
1951 => 52
1952 => 01
1953 => 53
1954 => 52
1955 => 52
1956 => 52
1957 => 01
1958 => 01
1959 => 53
1960 => 52
1961 => 52
1962 => 52
1963 => 01
1964 => 53
1965 => 52
1966 => 52
1967 => 52
1968 => 01
1969 => 01
1970 => 53
1971 => 52
1972 => 52
1973 => 52
1974 => 01
1975 => 01
1976 => 53
1977 => 52
1978 => 52
1979 => 52
1980 => 01
1981 => 53
1982 => 52
1983 => 52
1984 => 52
1985 => 01
1986 => 01
1987 => 53
1988 => 52
1989 => 52
1990 => 52
1991 => 01
1992 => 53
1993 => 52
1994 => 52
1995 => 52
1996 => 01
1997 => 01
1998 => 53
1999 => 52
2000 => 52
2001 => 52
2002 => 01
2003 => 01
2004 => 53
2005 => 52
2006 => 52
2007 => 52
2008 => 01
2009 => 53
2010 => 52
2011 => 52
2012 => 52
2013 => 01
2014 => 01
2015 => 53
2016 => 52
2017 => 52
2018 => 52
2019 => 01
2020 => 53
2021 => 52
2022 => 52
2023 => 52
2024 => 01
2025 => 01
2026 => 53
2027 => 52
2028 => 52
2029 => 52
2030 => 01
2031 => 01
2032 => 53
2033 => 52
2034 => 52
2035 => 52
2036 => 01
2037 => 53
2038 => 01
2039 => 01
2040 => 01
2041 => 01
2042 => 01
2043 => 01
2044 => 01
2045 => 01
2046 => 01
2047 => 01
2048 => 01
2049 => 01
2050 => 01
2051 => 01
2052 => 01
2053 => 01
2054 => 01
2055 => 01
2056 => 01
2057 => 01
2058 => 01
2059 => 01
2060 => 01
2061 => 01
2062 => 01
2063 => 01
2064 => 01
2065 => 01
2066 => 01
2067 => 01
2068 => 01
2069 => 01
2070 => 01
2071 => 01
2072 => 01
2073 => 01
2074 => 01
2075 => 01
2076 => 01
2077 => 01
2078 => 01
2079 => 01
2080 => 01
2081 => 01
2082 => 01
2083 => 01
2084 => 01
2085 => 01
2086 => 01
2087 => 01
2088 => 01
2089 => 01
2090 => 01
2091 => 01
2092 => 01
2093 => 01
2094 => 01
2095 => 01
2096 => 01
2097 => 01
2098 => 01
2099 => 01
If you were asking how to get the number of weeks left in the year, this would do it:如果您问如何获得一年中剩余的周数,可以这样做:
<?php
$year = date('Y');
$week_count = date('W', strtotime($year . '-12-31'));
if ($week_count == '01')
{
$week_count = date('W', strtotime($year . '-12-24'));
}
echo ($week_count - date('W'));
echo ' weeks left in ' . date('Y') . '!';
?>
Edit: Added logic to compensate for the '01' returned by date('W');编辑:添加逻辑以补偿 date('W'); 返回的 '01';
echo getLastWeekOfYear(2015);
function getLastWeekOfYear($year) {
$date = new DateTime();
return date('W', strtotime(date('Y-m-d', strtotime($date->setISODate($year, 1, "1")->format('Y-m-d') . "-1day"))));
}
With strtotime() you can count till 2037 cause the 2038 bug使用strtotime()你可以数到 2037 导致2038 错误
To be sure to count all weeks of a year you must count from the 28th December, because is the only day surely in the last week of the year.为了确保计算一年中的所有周,您必须从 12 月 28 日开始计算,因为这是一年中最后一周肯定的唯一一天。
<?php
for($year=2018;$year<2038;$year++) {
echo $year.' => ' .date("W",strtotime('28th December '.$year)) .' Weeks<br>';
}
?>
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.