Php convert week number to date
I'm trying to group together dates into a week number and year, and then I want to convert that week number back into a unix timestamp. How can I go about doing this? Show
asked Apr 23, 2011 at 9:10
1 I assume you are using ISO 8601 week numbers, and want the first day of a ISO 8601 week so that e.g.
Note that the week number needs to be two digits. Shamefully, answered Apr 23, 2011 at 9:27
PekkaPekka 434k137 gold badges965 silver badges1079 bronze badges 3
Then:
The answered Apr 23, 2011 at 9:17
OswaldOswald 30.6k3 gold badges40 silver badges68 bronze badges 6
Update: Instead of
answered Apr 23, 2011 at 9:16
FlorianFlorian 3,1351 gold badge27 silver badges38 bronze badges 2 Recently I've been building a little project that pulls data from Google Analytics and shows your web statistics in a simple form. One thing I wanted to do was show the data for a quarter, but graphing by day is too chaotic and graphing by month only gives three points, so I wanted to graph by week. This was fine but the data returned by Analytics only gives me the week numbers, and since this project is aimed at demystifying web stats for normal people, it really needs normal dates on it! Using DateTime::setISODateI found that the DateTime extension has a method setISODate() which accepts the year and week number, and makes a date you can then use as normal. $week_start = new DateTime(); $week_start->setISODate($year,$week_no); echo $week_start->format('d-M-Y'); You can also do this the other way around; if you have a date in PHP, there's the 'W' flag for date() which will return you the week number - very handy! To get the ISO week number (1-53) of a date represented by a Unix timestamp, use For a date represented by a DateTime instance, use To get the corresponding four-digit year (e.g. 2022), use Read more about strftime(), idate() and DateTime::format() in the PHP manual. To get the Unix timestamp representing the start of the week (Monday at midnight), use $year is a 4-digit year (e.g. 2022), and $week is an ISO week number (1-53). The sprintf() call
generates a week string in ISO 8601 format, e.g. "2022W01". To get the start of the week as a DateTime object, use Read more about strtotime() in the PHP manual. To get the number of ISO weeks (i.e. the number of the last week) in a year, use This is based on the fact that the last week of the year always includes 28 December. Week numbers in …How to get date from week number in php?getStartAndEndDate($week, $year); output: $return[0] = $firstDay; $return[1] = $lastDay; The return value will be something like an array in which the first entry is the week starting date and the second being the ending date.
How to get week php?Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.
How to get number of weeks in a year in php?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"; } ?>
How can I get last week start and end date in php?date("m/d/Y", strtotime("last week monday")); date("m/d/Y", strtotime("last week sunday")); It will give the date of Last week's Monday and Sunday.
|