php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66705 format inaccurate after setTimezone
Submitted: 2014-02-13 03:01 UTC Modified: 2014-02-13 04:04 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: chris at codewiz dot biz Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.5.3 OS: Ubuntu 13.10
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: chris at codewiz dot biz
New email:
PHP Version: OS:

 

 [2014-02-13 03:01 UTC] chris at codewiz dot biz
Description:
------------
---
From manual page: http://www.php.net/datetime.format
---
What is happening:

DateTime()->format() method strange behaviour after DateTime->setTimezone().
Ignores the public date property, Expected return value (format mask applied) should be based on the current public timezone and date properties.

Format method should apply the argument format mask to the date property based on current timezone property. Instead it appears that an unknown/undocumented modification to the date property is applied.

test script outputs;

$nowUtc

object(DateTime)[2607]
  public 'date' => string '2014-02-13 02:42:48' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

$nowUtc->format('Y-m-d h:i:s')

string '2014-02-13 02:42:48' (length=19)

$nowUtc->setTimezone( new \DateTimeZone( 'Australia/Sydney' ) )

object(DateTime)[2608]
  public 'date' => string '2014-02-13 13:42:48' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Australia/Sydney' (length=16)

$nowUtc->format('Y-m-d h:i:s')

string '2014-02-13 01:42:48' (length=19) // expected 2014-02-13 13:42:48 based on Australia/Sydney


What is 2014-02-13 01:42:48 from anyway!

Test script:
---------------
<?php
            $nowUtc = new \DateTime( 'now',  new \DateTimeZone( 'UTC' ) );
            echo '$nowUtc'.PHP_EOL;
            var_dump($nowUtc);
            $nowUtc = new \DateTime( 'now',  new \DateTimeZone( 'UTC' ) );
            echo '$nowUtc->format(\'Y-m-d h:i:s\')'.PHP_EOL;
            var_dump($nowUtc->format('Y-m-d h:i:s'));
            $nowUtc->setTimezone( new \DateTimeZone( 'Australia/Sydney' ) );
            echo '$nowUtc->setTimezone( new \DateTimeZone( \'Australia/Sydney\' ) )'.PHP_EOL;
            var_dump($nowUtc);
            echo '$nowUtc->format(\'Y-m-d h:i:s\')'.PHP_EOL;
            var_dump($nowUtc->format('Y-m-d h:i:s'));exit;
?>

Expected result:
----------------
format method should apply the argument format mask to the date property based on current timezone property.

Actual result:
--------------
Instead it appears that an unknown/undocumented modification to the date property is applied.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-02-13 03:03 UTC] chris at codewiz dot biz
-Operating System: +Operating System: Ubuntu 13.10 -PHP Version: Irrelevant +PHP Version: 5.5.3
 [2014-02-13 03:03 UTC] chris at codewiz dot biz
fixed version and os
 [2014-02-13 04:04 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-02-13 04:04 UTC] requinix@php.net
The "01" is because you used the 12-hour format 'h' instead of the 24-hour format 'H'.
http://www.php.net/date
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 21:00:01 2026 UTC