php / php/php-src

Respect timezone offset given after timezone name

Open
#16,276 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

Hello 👋 With the following code:

<?php
$date = new DateTime('2024-11-03 01:30:00 America/Toronto -0500');

echo $date->format('Y-m-d H:i:s e O');

Resulted in this output:

2024-11-03 01:30:00 America/Toronto -0400

https://3v4l.org/qg4aN

But I expected this output instead:

2024-11-03 01:30:00 America/Toronto -0500

Both exist, and it's possible to obtain the first with:

$date = new DateTime('@1730611800');
$date->setTimezone(new DateTimeZone('America/Toronto'));

As per the test above, it sounds like whatever offset coming after a named timezone like "America/Toronto" is ignored, so we can have the timezone set by name or by offset but we cannot have both while it's actually needed to distinguish 2 possibly different moments during DST such as Nov 3rd 1:30 -0400 and Nov 3rd 1:30 -0500 which both exist in America/Toronto.

A possible implementation would to:

  • Detect both named timezone and offset timezone,
  • if only one is present:
    • no change: keep current behavior
  • if both are present:
    • and the current date-time + timezone offset exist in the given timezone city
      • apply both
    • but the current date-time + timezone offset is invalid
      • ignore offset (i.e. current behavior)

Thanks 🙏

PHP Version

PHP 8.3.12 and also 8.4.0-dev (78767de197d1b2a6d2dd2757258b4c642bda8052)

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

Reproduce the behavior with the DateTime constructor using the America/Toronto name and -0500 offset, then compare it with the @timestamp and setTimezone example. Trace the date-time parsing entry point and add coverage for both valid November 3, 2024 offsets; done means the explicit offset is respected when it identifies an existing local time and current behavior remains for invalid combinations.

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
34/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.