Skip to content

Respect timezone offset given after timezone name #16276

Open

Description

@kylekatarnls

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 (78767de)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions