php / php/php-src

DateTime::diff() miscounts hours in days where a DST transition happens

Offen
#18,393 4 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@derickr arbeitet bereits daran.

Seit 22.4.2025.

Bug Extension: date Status: Needs Triage
Vorherrschende Sprache
C
Sterne
40.4k
Forks
8.1k
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
96

Beschreibung

Description

The following code:

<?php

$ref = new DateTime('2024-01-01 00:00:00', new DateTimeZone('Europe/Berlin'));
$t1 = new DateTime('2024-03-31 12:00:00', new DateTimeZone('Europe/Berlin'));
$t2 = new DateTime('2024-04-01 12:00:00', new DateTimeZone('Europe/Berlin'));
$t3 = new DateTime('2024-10-27 12:00:00', new DateTimeZone('Europe/Berlin'));
$t4 = new DateTime('2024-10-28 12:00:00', new DateTimeZone('Europe/Berlin'));

// DST start
$diff1 = $ref->diff($t1);
$diff2 = $ref->diff($t2);
// DST end
$diff3 = $ref->diff($t3);
$diff4 = $ref->diff($t4);

echo('DST start' . PHP_EOL);
var_dump($diff1->h, $diff2->h);

echo('DST end' . PHP_EOL);
var_dump($diff3->h, $diff4->h);

Resulted in this output:

DST start
int(11)
int(12)
DST end
int(12)
int(12)

But I expected this output instead:

DST start
int(12)
int(12)
DST end
int(12)
int(12)

https://3v4l.org/VkM09

As far as I understand it DateTime::diff() is supposed to count the time based on the local clock of a time zone.
But on the day of a forward time transition when DST starts (2024-03-31), DateTime::diff() only counts 11 hours when the time on the local clock is actually 12 o'clock.

This is also inconsistent with the day of a backwards time transition when DST ends (2024-10-27), where DateTime::diff() still counts 12 hours when actually 13 hours have elapsed.

This issue started with PHP version 8.1. Prior versions work correctly.

PHP Version

PHP 8.1.32 (cli) (built: Mar 13 2025 18:27:44) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.32, Copyright (c) Zend Technologies
with Zend OPcache v8.1.32, Copyright (c), by Zend Technologies

Operating System

Ubuntu 24.04.1

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.