How to Convert Week Number and Year into Unix Timestamp

How to convert week number and year into unix timestamp?

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.

UNIX date: How to convert week number (date +%W) to a date range (Mon-Sun)?

With GNU date:

$ cat weekof.sh
function weekof()
{
local week=$1 year=$2
local week_num_of_Jan_1 week_day_of_Jan_1
local first_Mon
local date_fmt="+%a %b %d %Y"
local mon sun

week_num_of_Jan_1=$(date -d $year-01-01 +%W)
week_day_of_Jan_1=$(date -d $year-01-01 +%u)

if ((week_num_of_Jan_1)); then
first_Mon=$year-01-01
else
first_Mon=$year-01-$((01 + (7 - week_day_of_Jan_1 + 1) ))
fi

mon=$(date -d "$first_Mon +$((week - 1)) week" "$date_fmt")
sun=$(date -d "$first_Mon +$((week - 1)) week + 6 day" "$date_fmt")
echo "\"$mon\" - \"$sun\""
}

weekof $1 $2
$ bash weekof.sh 12 2012
"Mon Mar 19 2012" - "Sun Mar 25 2012"
$ bash weekof.sh 1 2018
"Mon Jan 01 2018" - "Sun Jan 07 2018"
$


NOTE:

As the OP mentions, the week number is got by date +%W. According to GNU date's manual:

%W: week number of year, with Monday as first day of week (00..53)

So:

  1. Each week starts from Mon.
  2. If Jan 1 is Mon, then the first week will be week #1.
  3. If Jan 1 is not Mon, then the first few days will be week #0 and the week #1 starts from the first Mon.

Convert Year/Week to Timestamp (dd/mm/yyyy)

The weeks function in lubridate and str_c function in stringr might provide it:

df <- tribble(~year, ~week, 2010,1,2010,4,2010,5,2010,6)

df_tbl <- df %>%
mutate(beg = ymd(str_c(year, "-01-01")),
date_var = beg + weeks(week))

df_tbl$date_var

How to get week number since epoch? Like millis?

UNIX time is in seconds since January 1, 1970.

That day was a Thursday. So, midnight Sunday UTC on the zeroth week of the UNIX epoch (which started on on that date) occurred at -345 600 seconds in UNIX time. Midnight Sunday in the first week occurred at 259 200 seconds in UNIX time.

The UNIX time calendar contains no leap seconds, so each week is precisely 7 days, 604 800 seconds long.

Therefore, to get the week number in UTC from a UNIX time, do this.

(unixTime + 345600) div 604800

Javascript timestamps are UNIX timestamps in milliseconds. So, in JS, that's

Math.floor ((jsTimestamp + 345_600_000) / 604_800_000)

To get the Javascript timestamp of Sunday midnight back from the week number, reverse the operation.

(weekNumber * 604_800_000) + 345_600_000

This is all in UTC time.

Convert week number to a date

Check Date#commercial:

Date.commercial(2012, 20)
#=> Mon, 14 May 2012

How to calculate the day of the week based on unix time

The problem you ask is reasonably easy, compared to how ridiculously complicated other date/time functions can be (e.g. Zeller's congruence).

  1. Unix time is defined as the number of seconds elapsed after January 1, 1970, at 00:00 (midnight) UTC.

  2. You can look up a calendar to find that 1970-01-01 was a Thursday. There are 24 * 60 * 60 = 86400 seconds in a day.

  3. Therefore values 0 to 86399 are Thursday, 86400 to 172799 are Friday, 172800 to 259199 are Saturday, etc. These are blocks of 86400 seconds aligned at 0.

  4. Suppose T is your Unix timestamp. Then floor(T / 86400) tells you the number of days after 1970-01-01. 0 = Thursday January 1st; 1 = Friday January 2nd; 2 = Saturday January 3rd; etc.

  5. Add 4 and modulo 7. Now 0 → 4; 1 → 5; 2 → 6; 3 → 0; 4 → 1; 5 → 2; 6 → 3; 7 → 4; 8 → 5; 9 → 6; 10 → 0; etc. This is your final answer.

  6. In summary: day of week = (floor(T / 86400) + 4) mod 7.

  7. (This assumes that you want the day of week in UTC. If you want to calculate it for another time zone, you need to perform some addition or subtraction of hours and minutes on T first.)

strftime with week number format {YYYY}W{WW} gives wrong week

strtotime:

ISO year with ISO week YY "-"? "W" W "2008W27", "2008-W28"

strftime:

%W A numeric representation of the week of the year, starting with the
first Monday as the first week 46 (for the 46th week of the year
beginning with a Monday)

%V ISO-8601:1988 week number of the given year, starting with the
first week of the year with at least 4 weekdays, with Monday being the
start of the week 01 through 53 (where 53 accounts for an overlapping
week)

So probably you should use

echo $date = utf8_encode(strftime('%B %Y, week %V', strtotime('2015W38')));

Desclaimer: I am not proficient with php, so please do validate my thoughts.

EDIT>

As @syck adds: ISO 8601 counts weeks from 01 and the first week is the one with the year's first Thursday in it (see here).

Converting a string containing year and week number to datetime in Pandas

You need also define start day:

a = pd.to_datetime('2017_01_0',format = '%Y_%W_%w')
print (a)
2017-01-08 00:00:00

a = pd.to_datetime('2017_01_1',format = '%Y_%W_%w')
print (a)
2017-01-02 00:00:00

a = pd.to_datetime('2017_01_2',format = '%Y_%W_%w')
print (a)
2017-01-03 00:00:00

PHP week to quarter (date)

A date object can be created directly from a string such as '2022-W10' and the month can be determined using the format method. The calculation of the quarter is then just some school math.

$dateFromDb = '2022-W10';

$month = date_create($dateFromDb)->format('n');
$quarter = (int)(($month+2)/3); //int(1)

If the year is also required, this solution is available:

$dateFromDb = '2022-W49';

$dateTime = date_create($dateFromDb);
$quarter = (int)(($dateTime->format('n')+2)/3);

echo 'Quarter '.$quarter.' Year '.$dateTime->format('Y');


Related Topics



Leave a reply



Submit