Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
date() returns wrong week number for first day of year

Somehow the return value of the function date() makes no sense here. Does anybody know why?

Maybe I am missing something but according to the PHP Manual the following doesn't make sense:

echo date('W', strtotime("2021-01-01"));

output:

53

Shouldn't the first day of the year be in week 01? Other dates seem to be broken as well (e.g. 2021-01-05 returning 01 ). I checked the output of strtotime() but the UNIX timestamp seems to be correct. What am I missing here?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

PHP's DateTime format doc:

W - ISO-8601 week number of year, weeks starting on Monday

ISO-8601 definition of first week of the year:

Weeks start with Monday and end on Sunday. Each week's year is the Gregorian year in which the Thursday falls. The first week of the year, hence, always contains 4 January.

it return 53 because 01/01/2021 is in week 53 of year 2020.

If you do

echo date('W', strtotime("2021-01-04")); // first Monday of year 2021

it will return 01

over 4 years ago · Santiago Trujillo Report

0

Shouldn't the first day of the year be in week 01?

Like @catcon said, no.

To answer your comment,

This is correct but do you have an example of how to get the correct week number for the calendar year? In this case 1 for 2021-01-01

z = The day of the year (starting from 0)   0 through 365

From there, get the day of the year, subtract from 365 or 366, divide by 7, subtract from weeks in a year (365/7).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!