Given a week number, e.g. date -u +%W
, how do you calculate the days in that week starting from Monday?
Example rfc-3339 output for week 40:
2008-10-06
2008-10-07
2008-10-08
2008-10-09
2008-10-10
2008-10-11
2008-10-12
Given a week number, e.g. date -u +%W
, how do you calculate the days in that week starting from Monday?
Example rfc-3339 output for week 40:
2008-10-06
2008-10-07
2008-10-08
2008-10-09
2008-10-10
2008-10-11
2008-10-12
Another code hehe:
Since this question and the accepted answer were posted the DateTime classes make this much simpler to do:-
This has the added advantage of taking care of leap years etc..
See it working. Including the difficult weeks 1 and 53.
This function will give the timestamps of days of the week in which $date is found. If $date isn't given, it assumes "now." If you prefer readable dates to timestamps, pass a date format into the second parameter. If you don't start your week on Monday (lucky), pass in a different day for the third parameter.
So week_dates() should return something like...
I had the same question only using strftime instead of date as my starting point i.e. having derived a week number from strftime using %W I wanted to know the date range for that week - Monday to Sunday (or indeed any starting day). A review of several similar posts and in particular trying out a couple of the above approaches didn't get me to the solution I wanted. Of course I may have misunderstood something but I couldn't get what I wanted.
I would therefore like to share my solution.
My first thought was that given the description of strftime %W is:
if I established what the first Monday of each year is I could calculate an array of date ranges with an index equal to the value of %W. Thereafter I could call the function using strftime.
So here goes:
The Function:
Example:
Output:
I have tested this over several years including 2018 which is the next year when 1/1/2018 = Monday. Thus far seems to deliver the correct date range.
So I hope that this helps.
Regards
use as date() object from php date php
This calculation varies largely depending on where you live. For example, in Europe we start the week with a Monday, in US Sunday is the first day of the week. In UK week 1 is on Jan 1, others countries start week 1 on the week containing the first Thursday of the year.
You can find more general information at http://en.wikipedia.org/wiki/Week#Week_number