php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53833 DateTime::createFromFormat's DateTimeZone parameter ignored
Submitted: 2011-01-25 03:43 UTC Modified: 2011-01-25 10:26 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: alex dot joyce at staff dot comcen dot com dot au Assigned:
Status: Wont fix Package: Date/time related
PHP Version: 5.3.5 OS: Debian 5.0.3
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: alex dot joyce at staff dot comcen dot com dot au
New email:
PHP Version: OS:

 

 [2011-01-25 03:43 UTC] alex dot joyce at staff dot comcen dot com dot au
Description:
------------
I'm loading a DateTime object by a UNIX timestamp.

I assumed it would use the local timezone configured (e.g. date() behavior) but it defaults to UTC.

When passing a DateTimeZone object into the third parameter of DateTime::createFromFormat, it does nothing.

Setting the timezone using DateTime::setTimezone after the DateTime object has loaded gives the correct result.

See examples.

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

$time = 1293800400;
$tz = new DateTimeZone('Australia/Sydney');

// assumes local timezone
var_dump(date('r', $time));

// sets to "+00:00", should assume local timezone
var_dump(DateTime::createFromFormat('U', $time));

// sets to "+00:00", ignores given DateTimeZone object
var_dump(DateTime::createFromFormat('U', $time, $tz));  

// adjusts to given timezone correctly
var_dump(DateTime::createFromFormat('U', $time)->setTimezone($tz));

Expected result:
----------------
string(31) "Sat, 01 Jan 2011 00:00:00 +1100"

object(DateTime)#2 (3) {
  ["date"]=>
  string(19) "2011-01-01 00:00:00"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Australia/Sydney"
}

object(DateTime)#2 (3) {
  ["date"]=>
  string(19) "2011-01-01 00:00:00"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Australia/Sydney"
}

object(DateTime)#2 (3) {
  ["date"]=>
  string(19) "2011-01-01 00:00:00"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Australia/Sydney"
}

Actual result:
--------------
string(31) "Sat, 01 Jan 2011 00:00:00 +1100"

object(DateTime)#2 (3) {
  ["date"]=>
  string(19) "2010-12-31 13:00:00"
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+00:00"
}

object(DateTime)#2 (3) {
  ["date"]=>
  string(19) "2010-12-31 13:00:00"
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+00:00"
}

object(DateTime)#2 (3) {
  ["date"]=>
  string(19) "2011-01-01 00:00:00"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Australia/Sydney"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-25 10:26 UTC] derick@php.net
-Status: Open +Status: Wont fix
 [2011-01-25 10:26 UTC] derick@php.net
This behaviour is written down in the docs:
http://uk3.php.net/manual/en/datetime.construct.php

the @ also means a timezone of UTC here. If I would be writing this code again, then I would most likely not have done it like this; but changing it breaks BC (including some of my own code), so I don't think I would like to change it now.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 18:01:29 2024 UTC