php / php/php-src

DateTime setISODate() results in wrong timestamp?

未关闭
#13,267 9 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Extension: date Status: Needs Triage
主要语言
C
星标
40.4k
派生
8.1k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先,复现所提供的 PHP 8.2.15 脚本,重点关注 DateTime::setISODate()、时区处理以及保留的时间值。比较报告的执行时间下星期一和星期日的输出,然后检查相关的日期/时间实现和测试。完成的标准是确定这是否属于错误行为,并添加一个针对性的回归测试或记录预期的使用方式。

由索引模型根据 Issue 内容生成。

评估

技术栈
php
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。