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?

Php convert week number to date

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

Php convert week number to date

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!

How to get the week number from a date

To get the ISO week number (1-53) of a date represented by a Unix timestamp, use idate('W', $timestamp) or strftime('%-V', $timestamp).

For a date represented by a DateTime instance, use 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.

To get the corresponding four-digit year (e.g. 2022), use idate('o', $timestamp) or strftime('%G', $timestamp) or $dateTime->format('o').

Read more about strftime(), idate() and DateTime::format() in the PHP manual.

How to get the date from a week number

To get the Unix timestamp representing the start of the week (Monday at midnight), use strtotime(sprintf("%4dW%02d", $year, $week)).

$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 $date = new DateTime('midnight'); $date->setISODate($year, $week);

Read more about strtotime() in the PHP manual.

How to get the number of weeks in a year

To get the number of ISO weeks (i.e. the number of the last week) in a year, use idate('W', mktime(0, 0, 0, 12, 28, $year)).

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.