php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71833 DateTime - incorrect calculation for add()
Submitted: 2016-03-15 21:03 UTC Modified: 2016-03-15 23:35 UTC
From: troy_rudolph at bridge360 dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.6.19 OS: windows and linux
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: troy_rudolph at bridge360 dot com
New email:
PHP Version: OS:

 

 [2016-03-15 21:03 UTC] troy_rudolph at bridge360 dot com
Description:
------------
Adding a year to a DateTime produces different results for different timezone.

IMO, the test script below should produce two identical date/time strings.  However, the actual results are an hour apart.

Expected:
03/12/2016 12:00 am
03/12/2016 12:00 pm

Actual:
03/12/2016 12:00 am
03/11/2016 11:00 pm

Thank you!

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

$ct = new \DateTimeZone('America/Chicago');
$utc = new \DateTimeZone('UTC');
$oneYear = new \DateInterval('P1Y');

$date1 = \DateTime::createFromFormat('Y-m-d H:i:s', '2015-03-12 00:00:00', $ct);
$date1->add($oneYear);
echo "\n". $date1->format("m/d/Y h:i a");

$date2 = \DateTime::createFromFormat('Y-m-d H:i:s', '2015-03-12 00:00:00', $ct);
$date2->setTimezone($utc);
$date2->add($oneYear);
$date2->setTimezone($ct);
echo "\n". $date2->format("m/d/Y h:i a");



Expected result:
----------------
Expected:
03/12/2016 12:00 am
03/12/2016 12:00 pm

Actual result:
--------------
Actual:
03/12/2016 12:00 am
03/11/2016 11:00 pm

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-03-15 22:08 UTC] troy_rudolph at bridge360 dot com
-Status: Open +Status: Closed
 [2016-03-15 22:08 UTC] troy_rudolph at bridge360 dot com
I think this is open to interpretation
 [2016-03-15 23:35 UTC] requinix@php.net
-Status: Closed +Status: Not a bug
 [2016-03-15 23:35 UTC] requinix@php.net
It's a daylight savings thing. With America/Chicago, 3/12 12am was after daylight savings last year and before it this year, but since UTC doesn't do daylight savings there's an hour lost when you juggle the timezones and add a date interval that way. If you did the same thing on the other side of the transition then you would see an hour gained instead.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 23:00:02 2026 UTC