Bug #76624 [Nab]: date_create_from_format doesn't respect "T" Timezone format character

From: Date: Sun, 15 Jul 2018 14:50:44 +0000
Subject: Bug #76624 [Nab]: date_create_from_format doesn't respect "T" Timezone format character
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216340@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76624&edit=1 ID: 76624 Updated by: requinix@php.net Reported by: jalaj dot chhalotre at gmail dot com Summary: date_create_from_format doesn't respect "T" Timezone format character Status: Not a bug Type: Bug Package: Date/time related Operating System: Ubuntu 16.04.1 PHP Version: 7.0.30 Block user comment: N Private report: N New Comment: Any timezone abbreviation that is not unique will result in undefined behavior if you try to create a time using it. IST has three meanings. CST has three meanings. CDT has two meanings. Do not create a time using those abbreviations and expect PHP to correctly guess which timezone you meant. If you want to see the UTC offset like +05:30 then use the 'P' format. But you still must not use the abbreviation. Previous Comments: ------------------------------------------------------------------------ [2018-07-15 13:23:54] jalaj dot chhalotre at gmail dot com Oh ok, Thanks for highlighting Israel in IST. However my two observations: 1. Inconsitency is not specific to IST, for other timezones as well as mentioned in my previous comment. 2. When existing date formatted with T, Asia/Kolkata is printed as IST, but when date is created with mentioned function and format T, IST is interpreted as Israel time. Isn't it be canonical. My suggestion - if there are multiple representations, T format should output difference with UTC e.g. +5:30. The same way T format does for "Brazil/Acre" timezone. ------------------------------------------------------------------------ [2018-07-15 10:06:15] requinix@php.net Well spotted, @a. Indeed, there are three different "IST" timezones. Abbreviations are not unique. https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations ------------------------------------------------------------------------ [2018-07-15 09:56:45] a at b dot c dot de What timezone does IST name? Jerusalem, Kolkata, or Dublin? <?php $date2=date_create_from_format('Y-m-d H:i:s T', '2018-07-13 15:30:15 IST'); echo $date2->format('Y-m-d H:i:s e (T)'),"\t", $date2->format('U'),"\n"; echo "\n"; foreach(array_column(DateTimeZone::listAbbreviations()['ist'], 'timezone_id') as $tz) { $date1 = new DateTime('2018-07-13 15:30:15', new DateTimeZone($tz)); echo $date1->format('Y-m-d H:i:s e (T)'),"\t", $date1->format('U'),"\n"; } Apparently it's interpreted as Israel Standard Time. Suggest you avoid the ambiguity and stick with using "Asia/Kolkata". $date2=date_create_from_format('Y-m-d H:i:s T', '2018-07-13 15:30:15 Asia/Kolkata'); echo $date2->format('Y-m-d H:i:s e (T)'),"\t", $date2->format('U'),"\n"; ------------------------------------------------------------------------ [2018-07-15 08:07:00] jalaj dot chhalotre at gmail dot com Noticed this bug is timezone dependent, e.g. if TimeZone is CDT (Canada/Central) the difference in epoch of mentioned function is 0. On the other side IST, CST(China Standard time) result in epoch difference. Created a test function to check variability of this bug with timezone. <?php bug_epoch_diff('2018-07-13 15:30:15','Asia/Shanghai'); bug_epoch_diff('2018-07-13 15:30:15','Asia/Kolkata'); bug_epoch_diff('2018-07-13 15:30:15','Asia/Hong_Kong'); bug_epoch_diff('2018-07-13 15:30:15','Asia/Macau'); bug_epoch_diff('2018-07-13 15:30:15','Asia/Macau'); bug_epoch_diff('2018-07-13 15:30:15','Canada/Central'); bug_epoch_diff('2018-07-13 15:30:15','Brazil/Acre'); bug_epoch_diff('2018-07-13 15:30:15','Europe/Berlin'); function bug_epoch_diff($datetime,$timezone){ // date='2018-07-13 15:30:15' TimeZone='Asia/Kolkata' $date = new DateTime($datetime, new DateTimeZone($timezone)); echo "DateTime in format 'Y-m-d H:i:s T': ". $date->format('Y-m-d H:i:s T'); echo " | "; echo $ep1=$date->format('U'); echo " | "; //1531476015 echo $ep2=date_create_from_format('Y-m-d H:i:s T', $date->format('Y-m-d H:i:s T'))->format('U'); //1531488615 echo " | "; //Comment: for same date two different epoch echo "Epoch difference for ".$timezone."=".(((float)$ep2)-((float)$ep1))."<br>"; } ?> _____ Output: DateTime in format 'Y-m-d H:i:s T': 2018-07-13 15:30:15 CST | 1531467015 | 1531517415 | Epoch difference for Asia/Shanghai=50400 DateTime in format 'Y-m-d H:i:s T': 2018-07-13 15:30:15 IST | 1531476015 | 1531488615 | Epoch difference for Asia/Kolkata=12600 DateTime in format 'Y-m-d H:i:s T': 2018-07-13 15:30:15 HKT | 1531467015 | 1531467015 | Epoch difference for Asia/Hong_Kong=0 DateTime in format 'Y-m-d H:i:s T': 2018-07-13 15:30:15 CST | 1531467015 | 1531517415 | Epoch difference for Asia/Macau=50400 DateTime in format 'Y-m-d H:i:s T': 2018-07-13 15:30:15 CST | 1531467015 | 1531517415 | Epoch difference for Asia/Macau=50400 DateTime in format 'Y-m-d H:i:s T': 2018-07-13 15:30:15 CDT | 1531513815 | 1531513815 | Epoch difference for Canada/Central=0 DateTime in format 'Y-m-d H:i:s T': 2018-07-13 15:30:15 -05 | 1531513815 | 1531513815 | Epoch difference for Brazil/Acre=0 DateTime in format 'Y-m-d H:i:s T': 2018-07-13 15:30:15 CEST | 1531488615 | 1531488615 | Epoch difference for Europe/Berlin=0 ------------------------------------------------------------------------ [2018-07-14 20:43:14] jalaj dot chhalotre at gmail dot com Corrected OS (typo) ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=76624 -- Edit this bug report at https://bugs.php.net/bug.php?id=76624&edit=1

« previous php.bugs (#216340) next »