Edit report at http://bugs.php.net/bug.php?id=53833&edit=1

 ID:                 53833
 Updated by:         der...@php.net
 Reported by:        alex dot joyce at staff dot comcen dot com dot au
 Summary:            DateTime::createFromFormat's DateTimeZone parameter
                     ignored
-Status:             Open
+Status:             Wont fix
 Type:               Bug
 Package:            Date/time related
 Operating System:   Debian 5.0.3
 PHP Version:        5.3.5
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2011-01-25 03:43:59] 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"

}


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=53833&edit=1

Reply via email to