2010-07-23 121 views
15

請告訴我如何獲得一年中的最後幾周?PHP - 獲取上週的年份數

+1

什麼exac這是你的意思嗎?請更具體一些。 – 2010-07-23 15:05:49

+5

@Andy E:不一定......最後一週的數字可能是53.第01周的ISO-8601定義是第一個星期四的星期。 – Powerlord 2010-07-23 15:14:29

+1

我在之前的評論中有一個例子,但我認爲它是錯誤的,所以我刪除了它。長話短說:由於52周是364天,一年是365-366天,每隔幾年你最終會增加一週。 – Powerlord 2010-07-23 15:20:13

回答

14

由你想要的一年,歐洲/柏林你的時區替換2010:

<?php 
    date_default_timezone_set('Europe/Berlin'); 
    echo gmdate("W", strtotime("31 December 2010")); 
?> 

你會得到的值015253之一。


只是爲了好玩(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"; 
    } 
?> 

輸出:

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 
1

如果你問如何讓留在今年的週數,這將做到這一點:

<?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') . '!'; 

?> 

編輯:添加邏輯以補償日期('W')返回的'01';

+0

這可能不會產生預期的結果,因爲如問題評論中所述,上週可能是'01'。在這種情況下,你的代碼會在今年剩下幾周。您應該以編程方式將邊緣案例'01'(通過在倒數第二週加1)。 – 2010-07-23 15:41:41

+0

@paul我剛剛編輯了代碼,很確定這會正確地補償返回的'01'...在第二個想法中,當本週返回01時,它仍然會被扣掉...... sooo close;) – joshtronic 2010-07-23 16:12:41

+0

無論如何,這是沿着這些線的東西。 :) – 2010-07-23 21:54:24

36
function getIsoWeeksInYear($year) { 
    $date = new DateTime; 
    $date->setISODate($year, 53); 
    return ($date->format("W") === "53" ? 53 : 52); 
} 

o日期格式給出了ISO-8601年份數字。我們可以使用它,並且「無效」日期自動滾動以生成有效日期(2011-02-31給出2011-03-03),以確定給定年份是否有53周。如果沒有,那麼它必須有52個。

另請參閱date format charactersDateTime::setISODate()手冊頁。

+3

不錯,簡單 – Gordon 2012-01-26 13:26:38

20

ISO-8601規範中,它說的是12月28日總是在其當年的最後一週
此基礎上,我們可以簡單地創建日期,看看在什麼星期它是:

$dt = new DateTime('December 28th'); 
echo $dt->format('W'); # 52 

$dt = new DateTime('December 28th, 2009'); 
echo $dt->format('W'); # 53 

demo

...或者,如果你正在使用date()strtotime()功能:echo date('W', strtotime('December 28th')); # 52

-1
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")))); 
} 
相關問題