Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda