Bug #76057 [Opn]: For DateTime the properties 'timezone' and 'timezone_type' are not set on init

From: Date: Sun, 11 Mar 2018 12:18:39 +0000
Subject: Bug #76057 [Opn]: For DateTime the properties 'timezone' and 'timezone_type' are not set on init
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-214283@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76057&edit=1 ID: 76057 Updated by: heiglandreas@php.net Reported by: projectcleverweb at gmail dot com Summary: For DateTime the properties 'timezone' and 'timezone_type' are not set on init Status: Open Type: Bug Package: Date/time related Operating System: Linux PHP Version: 7.2.3 Block user comment: N Private report: N New Comment: You can get the timezone-type as follows: $timezone = $date->getTimezone(); $location = $timezone->getLocation(); $type = 2; if (false !== $location) { $type = '3'; } if (preg_match('/[\+\-]\d{2}:\d{2}/', $timezone->getName())) { $type = '1'; } As a timezone-object sets the timezone type depending on the given value you don't need to set the timezone-type explicitly. And when you are extending the DateTime-Class (hopefully DateTimeImmutable but that's a different story) you are free to add a method getTimezoneType using the above code. The internal properties of the DateTime-Objects are initialized after a var_dump which is a known thing and can not be fixed as easily as it seems. You should under no circumstances depend on them but use the available API. Previous Comments: ------------------------------------------------------------------------ [2018-03-08 18:34:53] projectcleverweb at gmail dot com Ok then, if they are not intended to be used they both should simply be protected or private properties; however, in this case how are you supposed to access $timezone_type? If you are writing your own serialization method (or in my case an extension of DateTime) you would have to make an exception for DateTime because even though it has a __set_state() method, that method requires the value of $timezone_type. See: https://3v4l.org/Klvo3 Ideally DateTimeZone (and possibly DateTime) would have a getTimezoneType() method. ------------------------------------------------------------------------ [2018-03-08 14:49:17] jhdxr@php.net This behavior (get timezone via property) is not expected, you should use getTimezone() to retrieve timezone. ------------------------------------------------------------------------ [2018-03-05 21:04:46] projectcleverweb at gmail dot com Description: ------------ In short, these to properties are not set when the class is instantiated, but do become set when the class is serialized OR when the property list is requested (such as via get_object_vars()). You can see the conditions when timezone_type will be set on this page by just searching in the browser for "timezone_type" https://github.com/php/php-src/blob/ef255c9f0f2c2ad1ea224ed215edb00f5bc205bd/ext/date/php_date.c#L2269 This issue is occurring on PHP 5.6 and later (shown in the test script URL) Test script: --------------- https://3v4l.org/ZhQX9 --- OR --- $test = new DateTime('2018-03-05 00:00:00', new DateTimeZone('UTC')); var_dump($test->timezone); // Notice: Undefined property: DateTime::$timezone get_object_vars($test); var_dump($test->timezone); // string(3) "UTC" Expected result: ---------------- string(3) "UTC" string(3) "UTC" Actual result: -------------- Notice: Undefined property: DateTime::$timezone in /in/BQD1t on line 5 NULL string(3) "UTC" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76057&edit=1

« previous php.bugs (#214283) next »