php / php/php-src

Wrong DateInterval when going from summer time to winter time

Open
#16,547 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: date Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php

$timezone = new \DateTimeZone('Europe/Paris');

$periodStart = (new \DateTimeImmutable('2024-10-21'))->setTimezone($timezone)->setTime(00, 00, 00);
$periodEnd = (new \DateTimeImmutable('2024-10-27'))->setTimezone($timezone)->setTime(23, 59, 59);

var_dump($periodStart);
var_dump($periodEnd);

var_dump($timezone->getOffset($periodStart));
var_dump($timezone->getOffset($periodEnd));

var_dump($periodStart->diff($periodEnd)->format('%y years %m months %a days %H hours %I minutes %s seconds'));

Resulted in this output:

object(DateTimeImmutable)#2 (3) {
  ["date"]=>
  string(26) "2024-10-21 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(12) "Europe/Paris"
}
object(DateTimeImmutable)#3 (3) {
  ["date"]=>
  string(26) "2024-10-27 23:59:59.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(12) "Europe/Paris"
}
int(7200) // UTC+2
int(3600) // UTC+1
string(54) "0 years 0 months 6 days 23 hours 59 minutes 59 seconds"

But I expected this output instead:

object(DateTimeImmutable)#2 (3) {
  ["date"]=>
  string(26) "2024-10-21 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(12) "Europe/Paris"
}
object(DateTimeImmutable)#3 (3) {
  ["date"]=>
  string(26) "2024-10-27 23:59:59.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(12) "Europe/Paris"
}
int(7200)
int(3600)
string(54) "0 years 0 months 7 days 0 hours 59 minutes 59 seconds"

I did the same test using winter to summer time switch and the result is good.

<?php

$timezone = new \DateTimeZone('Europe/Paris');

$periodStart = (new \DateTimeImmutable('2024-03-25'))->setTimezone($timezone)->setTime(00, 00, 00);
$periodEnd = (new \DateTimeImmutable('2024-03-31'))->setTimezone($timezone)->setTime(23, 59, 59);

var_dump($periodStart);
var_dump($periodEnd);

var_dump($timezone->getOffset($periodStart));
var_dump($timezone->getOffset($periodEnd));

var_dump($periodStart->diff($periodEnd)->format('%y years %m months %a days %H hours %I minutes %s seconds'));

Output:

object(DateTimeImmutable)#2 (3) {
  ["date"]=>
  string(26) "2024-03-25 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(12) "Europe/Paris"
}
object(DateTimeImmutable)#3 (3) {
  ["date"]=>
  string(26) "2024-03-31 23:59:59.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(12) "Europe/Paris"
}
int(3600)
int(7200)
string(54) "0 years 0 months 6 days 22 hours 59 minutes 59 seconds"

3v4l.org links:

PHP Version

PHP 8.2.21 and up

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied DateTimeImmutable::diff reproduction for the Europe/Paris summer-to-winter transition and compare it with the winter-to-summer case. Trace the DateTimeImmutable::diff behavior and related DateTimeZone offset handling; done means the interval matches the stated expected output and the transition cases remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.