php / php/php-src

DateTime setISODate() results in wrong timestamp?

Ouverte
#13,267 9 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Extension: date Status: Needs Triage
Langage dominant
C
Étoiles
40.4k
Forks
8.1k
Merge moyen
2 j 13 h
PR mergées (30 j)
96

Description

Description

I call every Monday a cronjob at 00:01. I want known the year and week from last week.

<?php
$dt = new DateTime('last week monday 00:00:01', new DateTimeZone('Europe/Berlin'));
$year = (int)$dt->format('Y');
$week = (int)$dt->format('W');

// After this, I call a second script with parameters ($year and $week) few times for different tasks

$dto = new DateTime();
$dto->setISODate($year, $week);

$fmt = new IntlDateFormatter('de_DE', IntlDateFormatter::NONE, IntlDateFormatter::NONE, 'Europe/Berlin');
$fmt->setPattern('LLL dd');

// Hint: $dto->getTimestamp() is the date a week ago, but with current time. So its always the time its called.

$week_start = $fmt->format($dto->getTimestamp());
$dto->modify('+6 days'); // for sunday
$week_end = $fmt->format($dto->getTimestamp());

echo $week_start . ' - ' . $week_end;

Resulted in this output:

Jan 23 - Jan 29

But I expected this output instead, for Timezone 'Europe/Berlin':

Jan 22 - Jan 28

The parameters $year and $week are only determined once. With these two values, the same script is called several times to create each time a graphic containing the date for the Monday and Sunday of the previous week. The script is called by a cronjob 00:01 every Monday. The graphics were created on the server at 00:01:23 and later. The script needs less than 1 second to create the graphics.

So I remove the images and call the script again ~9 o'clock and its shows me now the correct values "Jan 22 - Jan 28".
Is this a bug or am I doing something wrong here?

Because its worked for many years, but since 2024 its startet with wrong dates (one date shifted). Executed/Generated on:
✗ 03.01. wrong date for last week December.
✗ 08.01. again wrong date for first week January
15.01. shows the correct dates! "08 - 14"
22.01. shows the correct dates! "15 - 21"
✗ 29.01. again wrong date "23 - 29" must be "22 - 28"

So its strange.

Server timezone with timedatectl shows:

Local time: Mon 2024-01-29 10:53:00 CET
Universal time: Mon 2024-01-29 09:53:00 UTC
RTC time: Mon 2024-01-29 09:53:00
Time zone: Europe/Berlin (CET, +0100)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

PHP Version

PHP 8.2.15

Operating System

AlmaLinux 8.9

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par reproduire le script PHP 8.2.15 fourni autour de DateTime::setISODate(), de la gestion des fuseaux horaires et de la valeur d’heure conservée. Comparez la sortie du lundi et du dimanche aux heures d’exécution indiquées, puis examinez l’implémentation et les tests pertinents relatifs aux dates et aux heures. Le travail est terminé lorsqu’il est établi s’il s’agit d’un comportement incorrect et qu’un test de régression ciblé est ajouté ou que l’utilisation attendue est documentée.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
php
Domaine
backend
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.