|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-03-05 21:04 UTC] 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" PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 12:00:02 2025 UTC |
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.