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?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
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:Update: Instead of
1/1/2011
use the first monday in 2011. The 2nd calculation is not needed anymore.$week
: The week number$year
: The year numberThen:
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.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
returnsJanuary 3 2011
.strtotime
can do this out of the box using the{YYYY}W{WW}
format: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.