php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81074 DateTime::setTime causes unexpected offset change
Submitted: 2021-05-23 09:57 UTC Modified: 2021-08-08 11:49 UTC
From: php-bugs at allenjb dot me dot uk Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 8.0.6 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php-bugs at allenjb dot me dot uk
New email:
PHP Version: OS:

 

 [2021-05-23 09:57 UTC] php-bugs at allenjb dot me dot uk
Description:
------------
When using ->setTime() on a DateTime object, the offset may be changed even if the new time is valid in the existing offset.

(Aside: This bug also reveals that with the current API it's actually impossible to use ->setTime() to set some valid time values)

Test script:
---------------
<?php

$tzUK = new DateTimeZone("Europe/London");
$tzUtc = new DateTimeZone("UTC");
$dt = DateTime::createFromFormat("!Y-m-d H:i:s", "2020-10-25 00:05:00", $tzUtc);
$dt = DateTime::createFromFormat('U', $dt->format('U'));

print $dt->format(DateTime::RFC3339 ." e") ."\n";

$dt->setTimeZone($tzUK);

print $dt->format(DateTime::RFC3339 ." e") ."\n";

$dt->setTime((int) $dt->format('H'), (int) $dt->format('i'), 0);

print $dt->format(DateTime::RFC3339 ." e") ."\n";

Expected result:
----------------
2020-10-25T00:05:00+00:00 +00:00
2020-10-25T01:05:00+01:00 Europe/London
2020-10-25T01:05:00+01:00 Europe/London

Actual result:
--------------
2020-10-25T00:05:00+00:00 +00:00
2020-10-25T01:05:00+01:00 Europe/London
2020-10-25T01:05:00+00:00 Europe/London

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-23 09:58 UTC] php-bugs at allenjb dot me dot uk
Forgot to mention: I tested this on current master this morning and the issue still occurs there.
 [2021-08-08 11:49 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2021-08-08 11:49 UTC] derick@php.net
This is (now) expected behaviour. It prefers the non-DST time.

You're correct that saying that you can't chose to pick one over the other, but that's a separate issue, and documented in https://wiki.php.net/rfc/datetime_and_daylight_saving_time#backward_transitions
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Feb 05 20:01:30 2025 UTC