|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 07:00:01 2025 UTC |
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/>';