Php week number in year

PHP — get last week number in year

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

@R. Bemrose I know that, I was just being sarcastic because of the lack of information in the question.

7 Answers 7

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.

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 

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

Читайте также:  Timer python 3 примеры

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

You’ll get one of the values 01 , 52 or 53 .

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 

Источник

Week numbers in PHP

To get the ISO week number (1-53) of a date represented by a Unix timestamp, use idate(‘W’, $timestamp ) or strftime(‘%-V’, $timestamp ) .

For a date represented by a DateTime instance, use intval( $dateTime ->format(‘W’)) . Note that format() returns the week number zero-prefixed (e.g. 05), so you need intval to strip the leading zero.

To get the corresponding four-digit year (e.g. 2023), use idate(‘o’, $timestamp ) or strftime(‘%G’, $timestamp ) or $dateTime ->format(‘o’) .

Read more about strftime(), idate() and DateTime::format() in the PHP manual.

How to get the date from a week number

To get the Unix timestamp representing the start of the week (Monday at midnight), use strtotime(sprintf(«%4dW%02d», $year , $week )) .

$year is a 4-digit year (e.g. 2023), and $week is an ISO week number (1-53). The sprintf() call generates a week string in ISO 8601 format, e.g. «2023W01».

To get the start of the week as a DateTime object, use $date = new DateTime(‘midnight’); $date->setISODate( $year , $week );

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.

A lot of software that deal with dates has poor support for week number, in particular the ISO week numbering scheme that is used on this site. The concept of week numbers is virtually unknown in large parts of the world, so adding support for them is not high on the agenda for most software companies. However, with a few tricks it is often possible to make week numbers calculations, even if the application does not have native support for week numbers.

These pages explain how to use week numbers in various popular software applications and programming languages.

Can we use your data to tailor ads for you?

Our partners will collect data and use cookies for ad personalization and measurement.

Источник

Get week number (in the year) from a date PHP

I want to take a date and work out its week number. So far, I have the following. It is returning 24 when it should be 42.

Just to add, don’t forget to set the timezone, using PHP.ini or via date_default_timezone_set(‘Asia/Singapore’);

19 Answers 19

Today, using PHP’s DateTime objects is better:

format("W"); echo "Weeknummer: $week"; 

It’s because in mktime() , it goes like this:

mktime(hour, minute, second, month, day, year); 

Hence, your order is wrong.

You can use Carbon, «github.com/briannesbitt/Carbon» to maximize your skills in Date manipulation. For carbon you can do that by: Carbon::createFromFormat(‘Y-m-d H’, ‘2012-10-18’)->format(‘W’);

@lukaserat How is that any different from what the accepted answer uses (besides the fact OP has to include an external dependency for something that is already native in the language)?

I would say that the order in mktime() is wrong, not the OP’s order. Of course, the latter is easier to change.

If the last week of December is also the first week of next year, (like in 2019), then you will get week number as 1, for dates falling in that week (like 30, 31)

$date_string = "2012-10-18"; echo "Weeknummer: " . date("W", strtotime($date_string)); 

This get today date then tell the week number for the week

Might be a little simpler than all that lot.

Other things you could do:

Becomes more difficult when you need year and week.
Try to find out which week is 01.01.2017.
(It is the 52nd week of 2016, which is from Mon 26.12.2016 — Sun 01.01.2017).

After a longer search I found

strftime('%G-%V',strtotime("2017-01-01")) 

https://www.php.net/manual/de/function.strftime.php
ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week. (01 through 53)

The equivalent in mysql is DATE_FORMAT(date, ‘%x-%v’) https://www.w3schools.com/sql/func_mysql_date_format.asp
Week where Monday is the first day of the week (01 to 53).

Could not find a corresponding solution with DateTime.
At least not without solutions like «+1day, last monday».

Edit: since strftime is now deprecated, maybe you can also use date. Didn’t verify it though.

This is a neat solution for year and week, though unfortunately strftime() has been deprecated as of PHP 8.1.0 php.net/manual/en/function.strftime.php

I have tried to solve this question for years now, I thought I found a shorter solution but had to come back again to the long story. This function gives back the right ISO week notation:

/** * calcweek("2018-12-31") => 1901 * This function calculates the production weeknumber according to the start on * monday and with at least 4 days in the new year. Given that the $date has * the following format Y-m-d then the outcome is and integer. * * @author M.S.B. Bachus * * @param date-notation PHP "Y-m-d" showing the data as yyyy-mm-dd * @return integer **/ function calcweek($date) < // 1. Convert input to $year, $month, $day $dateset = strtotime($date); $year = date("Y", $dateset); $month = date("m", $dateset); $day = date("d", $dateset); $referenceday = getdate(mktime(0,0,0, $month, $day, $year)); $jan1day = getdate(mktime(0,0,0,1,1,$referenceday[year])); // 2. check if $year is a leapyear if ( ($year%4==0 && $year%100!=0) || $year%400==0) < $leapyear = true; >else < $leapyear = false; >// 3. check if $year-1 is a leapyear if ( (($year-1)%4==0 && ($year-1)%100!=0) || ($year-1)%400==0 ) < $leapyearprev = true; >else < $leapyearprev = false; >// 4. find the dayofyearnumber for y m d $mnth = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334); $dayofyearnumber = $day + $mnth[$month-1]; if ( $leapyear && $month > 2 ) < $dayofyearnumber++; >// 5. find the jan1weekday for y (monday=1, sunday=7) $yy = ($year-1)%100; $c = ($year-1) - $yy; $g = $yy + intval($yy/4); $jan1weekday = 1+((((intval($c/100)%4)*5)+$g)%7); // 6. find the weekday for y m d $h = $dayofyearnumber + ($jan1weekday-1); $weekday = 1+(($h-1)%7); // 7. find if y m d falls in yearnumber y-1, weeknumber 52 or 53 $foundweeknum = false; if ( $dayofyearnumber 4 ) < $yearnumber = $year - 1; if ( $jan1weekday = 5 || ( $jan1weekday = 6 && $leapyearprev )) < $weeknumber = 53; >else < $weeknumber = 52; >$foundweeknum = true; > else < $yearnumber = $year; >// 8. find if y m d falls in yearnumber y+1, weeknumber 1 if ( $yearnumber == $year && !$foundweeknum) < if ( $leapyear ) < $i = 366; >else < $i = 365; >if ( ($i - $dayofyearnumber) < (4 - $weekday) ) < $yearnumber = $year + 1; $weeknumber = 1; $foundweeknum = true; >> // 9. find if y m d falls in yearnumber y, weeknumber 1 through 53 if ( $yearnumber == $year && !$foundweeknum ) < $j = $dayofyearnumber + (7 - $weekday) + ($jan1weekday - 1); $weeknumber = intval( $j/7 ); if ( $jan1weekday >4 ) < $weeknumber--; >> // 10. output iso week number (YYWW) return ($yearnumber-2000)*100+$weeknumber; > 

I found out that my short solution missed the 2018-12-31 as it gave back 1801 instead of 1901. So I had to put in this long version which is correct.

Источник

Оцените статью