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?
-
Please clarify what you mean by "convert a week number into a timestamp" – Pekka Apr 23 '11 at 10:08
3 Answers
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.

- 442,112
- 142
- 972
- 1,088
-
While the note about `DateTime::createFromFormat()` is still true today unfortunately, I want to add that you can still use DateTime for that: `new DateTime('2011W01')` does work. – Dennis98 May 03 '19 at 08:04
-
While this works... do you know where the official documentation is for this? strtotime (https://www.php.net/manual/en/function.strtotime.php) points you at DateFormats (https://www.php.net/manual/en/datetime.formats.date.php) which doesn't mention this format. Although I see Date (https://www.php.net/manual/en/function.date.php) does. – Anthony Jan 07 '20 at 12:51
-
Documentation here: https://www.php.net/manual/en/datetime.formats.compound.php Localized notations: ISO year with ISO week YY "-"? "W" W "2008W27", "2008-W28" – woens Feb 18 '22 at 09:23
$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.

- 31,254
- 3
- 43
- 68
-
I don't think this is reliable: `1/1/2011` was still in week 52/2010 according to ISO 8601. – Pekka Apr 23 '11 at 09:19
-
@Pekka This only matters if the conversion should be reversible in a trivial manner. Changed it nevertheless. – Oswald Apr 23 '11 at 09:23
-
This returns January 4, 2008 for week 1 of 2008, but that week starts December 31, 2007 according to ISO. – Pekka Apr 23 '11 at 09:48
-
I'd not recommend this solution. There are few places where I'd suggest using `mktime()` or `gmmktime()` in place of `strtotime()` – James C Apr 23 '11 at 10:01
-
@Pekka A week is a time span, so it can, by definition, not be converted into a timestamp (i.e. a single point in time). The only thing that can be done is take a timestamp that represents that week. This representative does not need to be the beginning of the week (unless otherwise stated in the requirements). – Oswald Apr 23 '11 at 10:06
-
@Oswald well, fair point. The OP indeed needs to clarify what they want. – Pekka Apr 23 '11 at 10:07
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.

- 3,145
- 1
- 27
- 38
-
I don't think this is reliable: `1/1/2011` was still in week 52/2010 according to ISO 8601. – Pekka Apr 23 '11 at 09:17
-