php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54799 Incorrect results when operating with time on DST changes
Submitted: 2011-05-17 16:57 UTC Modified: 2011-11-21 02:43 UTC
From: robertoherreros at gmail dot com Assigned: derick (profile)
Status: Duplicate Package: Date/time related
PHP Version: 5.3.6 OS: Linux
Private report: No CVE-ID: None
 [2011-05-17 16:57 UTC] robertoherreros at gmail dot com
Description:
------------
Incorrect results when operating with time on DST changes.

Operations like: '-1 hour', '-1 min' or setTime()

In order to resolve the problem, all operations with time must be done in timestamp directly.

Test script:
---------------
// 2011-03-27, 02:00:00 -> 03:00:00 
$tz = new DateTimezone('Europe/Madrid');
$d = new DateTime('2011-03-28 02:30:00',$tz);

// OK
// One day early
// Result: 2011-03-27 03:30:00
echo $d->modify('-1 day')->format('Y-m-d H:i:s').'<br/>';

// BAD
// One hour early (or other TIME operation)
// Expected: 2011-03-27 01:30:00, Actual: 2011-03-27 03:30:00
echo $d->modify('-1 hour')->format('Y-m-d H:i:s').'<br/>';

// BAD
// SetDate and setTime
// Expected: 2011-03-27 03:30:00, Actual: 2011-03-27 02:30:00
echo $d->setDate(2011,3,27)->setTime(2,30,0)->format('Y-m-d H:i:s').'<br/>';

// OK
// Set timezone again after setDate and setTime fix the above problem
// Result: 2011-03-27 03:30:00
echo $d->setDate(2011,3,27)->setTime(2,30,0)->setTimezone($tz)->format('Y-m-d H:i:s').'<br/>';

// OK
// One hour early but changing timestamp
// Result: 2011-03-27 01:30:00
echo $d->setTimestamp(($d->getTimestamp()-3600))->format('Y-m-d H:i:s').'<br/>';

// OK
// One hour after
// Result: 2011-03-27 03:30:00
echo $d->modify('2011-03-27 01:30:00')->modify('+1 hours')->format('Y-m-d H:i:s').'<br/>';


// BAD
// Two hours after
// Expected: 2011-03-27 04:30:00, Actual: 2011-03-27 03:30:00
echo $d->modify('2011-03-27 01:30:00')->modify('+2 hours')->format('Y-m-d H:i:s').'<br/>';

Expected result:
----------------
2011-03-27 03:30:00
2011-03-27 01:30:00
2011-03-27 03:30:00
2011-03-27 03:30:00
2011-03-27 01:30:00
2011-03-27 03:30:00
2011-03-27 04:30:00

Actual result:
--------------
2011-03-27 03:30:00
2011-03-27 03:30:00
2011-03-27 02:30:00
2011-03-27 03:30:00
2011-03-27 01:30:00
2011-03-27 03:30:00
2011-03-27 03:30:00

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-17 18:05 UTC] derick@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: derick
 [2011-05-18 08:34 UTC] robertoherreros at gmail dot com
One more thing:

// BAD
// 2011-03-27, 02:00:00 -> 03:00:00
// Expected: 2011-03-27 03:30:00, Actual: 2011-03-27 02:30:00
// 2011-03-27 02:30:00 don't exists
$tz = new DateTimezone('Europe/Madrid');
$d = new DateTime('2011-03-27 02:30:00',$tz);
echo $d->format('Y-m-d H:i:s').'<br/>';
 [2011-11-21 02:43 UTC] danielc@php.net
-Status: Assigned +Status: Duplicate
 [2011-11-21 02:43 UTC] danielc@php.net
This is a duplicate of https://bugs.php.net/bug.php?id=51051
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC