Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda