开发者

PHP - get last week number in year

Tell 开发者_开发技巧me please how to get the last number of weeks in a 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. 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. If does not, then it must have 52.

See also, date format characters and DateTime::setISODate() manual pages.


In ISO-8601 specification, it says that December 28th is always in the last week of its year.
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

demo

... or if you are using date() and strtotime() functions: echo date('W', strtotime('December 28th')); # 52


Replace 2010 by the year you want and Europe/Berlin with your timezone:

<?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.


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');


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

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.

<?php
for($year=2018;$year<2038;$year++) {
    echo $year.' => ' .date("W",strtotime('28th December '.$year)) .' Weeks<br>';
}
?>

Result:

  • 2018 => 52 Weeks
  • 2019 => 52 Weeks
  • 2020 => 53 Weeks
  • 2021 => 52 Weeks
  • 2022 => 52 Weeks
  • 2023 => 52 Weeks
  • 2024 => 52 Weeks
  • 2025 => 52 Weeks
  • 2026 => 53 Weeks
  • 2027 => 52 Weeks
  • 2028 => 52 Weeks
  • 2029 => 52 Weeks
  • 2030 => 52 Weeks
  • 2031 => 52 Weeks
  • 2032 => 53 Weeks
  • 2033 => 52 Weeks
  • 2034 => 52 Weeks
  • 2035 => 52 Weeks
  • 2036 => 52 Weeks
  • 2037 => 53 Weeks


As stated above, using December 28th instead of 31th is the way. Here is how I use it:

    for ($i = 2022; $i <= 2040; $i++) {
       $test = date_create_from_format('Y-m-d H:i:s', "{$i}-12-28 23:59:00", new DateTimeZone('Europe/Zurich'))->format('W');
       echo "$i -> $test\n";
    }

/*
2022 -> 52
2023 -> 52
2024 -> 52
2025 -> 52
2026 -> 53
2027 -> 52
2028 -> 52
2029 -> 52
2030 -> 52
2031 -> 52
2032 -> 53
2033 -> 52
2034 -> 52
2035 -> 52
2036 -> 52
2037 -> 53
2038 -> 52
2039 -> 52
2040 -> 52
*/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜