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?
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. Week 1 of 2011
returns January 3 2011
.
strtotime
can do
this out of the box using the {YYYY}W{WW}
format:
echo date["Y-m-d", strtotime["2011W01"]]; // 2011-01-03
Note that the week number needs to be two digits.
Shamefully, DateTime::createFromFormat
, the fancy new PHP 5 way of dealing with dates, seems unable to parse this kind of information - it doesn't have a "week" placeholder.
answered Apr 23, 2011 at 9:27
PekkaPekka
434k137 gold badges965 silver badges1079 bronze badges
3
$week
: The week number$year
: The year number
Then:
$timestamp = gmmktime [0, 0 , 0 , 1, , 4 + 7*[$week - 1], $year];
The 4 + 7*[$week - 1]
comes from the fact that
according to ISO 8601, the first week of the year is the one that contains January 4th.
answered Apr 23, 2011 at 9:17
OswaldOswald
30.6k3 gold badges40 silver badges68 bronze badges
6
strtotime['1/1/2011 + 4 weeks']
[1/1 ist always in week number one; this would bring me to week number five]. if you want any timestamp in the week then that's all you need, else you would have to go to the monday in this week:
$t = strtotime['1/1/2011 + 4 weeks'];
$t -= 24 * 60 * 60 * date['w', $t];
Update: Instead of
1/1/2011
use the first monday in 2011. The 2nd calculation is not needed anymore.
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::setISODate
I 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.How to get the week number from a date
idate['W', $timestamp]
or strftime['%-V', $timestamp]
.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.idate['o', $timestamp]
or strftime['%G', $timestamp]
or $dateTime->format['o']
.How to get the date from a week number
strtotime[sprintf["%4dW%02d", $year, $week]]
.$date = new DateTime['midnight']; $date->setISODate[$year, $week];
How to get the number of weeks in a year
idate['W', mktime[0, 0, 0, 12, 28, $year]]
.