PHP - get last week number in year

get all weeks of the year php
php get year from date
php get week number of month
php get month from date
php week number
php week number starting sunday
php string to date
php date

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.

PHP - get last week number in year, This week is . The first week of the year is the week that contains that year's first Thursday (='First 4-day week'). PHP. $weekNumber = date("W");. or date("W", epoch) for other week numbers. Remember getInstance(); now.get(Calendar. Read more about strtotime() in the PHP manual. How to get the number of weeks in a year. To get the number of ISO weeks (i.e. the number of the last week) in a year, use idate('W', mktime(0, 0, 0, 12, 28, $year)). This is based on the fact that the last week of the year always includes 28 December.

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

What's the Current Week Number?, W, ISO-8601 week number of year, weeks starting on Monday, Example: 42 (the 42nd to use date() and mktime() together to find dates in the future or the past. ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0) Examples: 1999 or 2003: Y

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

date - Manual, Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. The way to get what people would generally be looking for when they say strtotime() also returns time by year and weeknumber. Home » Get week starting and ending date in PHP Get week starting and ending date in PHP This is how you can get the week starting date or ending date by giving a week number and the year.

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

strtotime - Manual, This method calculates the beginning and ending of a week given the year and week number. The problem I've run into is that “first day of the  Today 2017-01-01 is 52nd week of 2016 year because 2016 was a leap year. So the function from this method will return wrong dates. – undefinedman Jan 1 '17 at 14:27. Absolutely NOT working, I've just resolved a week shift issue involving a direct copy and past of this solution.

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"))));
}

PHP Get First and Last Day of Week by Week Number, The ISO week date system is effectively a leap week calendar system that is part of the ISO In the period 4 January to 28 December the ISO week year number is always The last week of the ISO week-numbering year, i.e. W52 or W53, is the week All other week-numbering years are short years and have 52 weeks. Fetch Last WEEK Record Get Last 7 Day Record. Using the below MySQL query for fetching the last 7 days records from the mysql database table. If you want to get the last 10 days or the last 15 days records from a database table, you can change the query accordingly.

ISO week date, Getting the week number for a date in PHP is simple. Just use the powerful date() function that takes a format string and an optional unix timestamp as arguments. MySQL YEARWEEK() function Last update on February 26 2020 08:08:23 (UTC/GMT +8 hours)

Week number in PHP, year. Last update on February 26 2020 08:09:35 (UTC/GMT +8 hours) Write a PHP function to get start and end date of a week (by week number) of a <?​php function Start_End_Date_of_a_week($week, $year) { $time  There are 52 weeks in 2019. All weeks are starting on Monday and ending on Sunday. Please note that there are multiple systems for week numbering, this is the ISO week date standard (ISO-8601), other systems use weeks starting on Sunday (US) or Saturday (Islamic). The current week ( week 39) is highlighted.

PHP Date Exercise: Start and end date of a week(by week number , The issue comes from line 202 in src/DateArgumentWrapper.php Because it is using ISO-8601 week number (i.e. 'W'), the new week. In doing some research, I found out that December 28th is always in the last week of the year. I have uploaded a patch that changes the date used for checking how  Week 20 is from Monday, May 11, 2020 until (and including) Sunday, May 17, 2020. Week number according to the ISO-8601 standard, weeks starting on Monday. The first week of the year is the week that contains that year's first Thursday (='First 4-day week'). ISO representation: 2020-W20. The highest week number in a year is either 52 or 53.

Comments
  • What exactly do you mean by that? Please be more specific.
  • @Andy E: Not necessarily... the last week number could be 53. The ISO-8601 definition of week 01 is the week with the first Thursday in it.
  • I had an example in my previous comment, but I think it was wrong, so I removed it. Long story short: Since 52 weeks are 364 days, and a year is 365-366 days, every few years you end up with an extra week.
  • do you mean the number of weeks left in the year?
  • @R. Bemrose I know that, I was just being sarcastic because of the lack of information in the question.
  • That might not yield an expected result, because as stated in the question comments, the last week may be 01. In such a case, your code would yield a negative number of weeks left in the year. You should programmatically convert the edge case 01 (by adding 1 to the next-to-last week).
  • @paul I just edited the code, pretty sure that would compensate for the returned '01' correctly... on second thought, it would still bunk out when the current week returns 01 as well... sooo close ;)
  • It's something along those lines, anyway. :)