Bug #76624 [Opn->Nab]: date_create_from_format doesn't respect "T" Timezone format character
| From: | requinix@php.net | Date: | Sun, 15 Jul 2018 10:06:16 +0000 |
| Subject: | Bug #76624 [Opn->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-216337@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: Open
+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:
Well spotted, @a.
Indeed, there are three different "IST" timezones. Abbreviations are not unique.
https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations
Previous Comments:
------------------------------------------------------------------------
[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)
------------------------------------------------------------------------
[2018-07-14 20:39:49] jalaj dot chhalotre at gmail dot com
Description:
------------
When T timezone format character is used to specify timezone in date_create_from_format() function,
wrong time (with respect to epoch) is generated.
In test script attached, two different epoch times are generated if 'T' and 'e'
timezone format characters are used. However using e format character returns correct time.
Test script:
---------------
# date_string = "2018-07-13 15:30:15 IST" = "2018-07-13 15:30:15 Asia/Kolkata"
$date1 = new DateTime('2018-07-13 15:30:15', new DateTimeZone('Asia/Kolkata'));
echo $date1->format('Y-m-d H:i:s T');
//2018-07-13 15:30:15 IST
echo $str1=$date1->format('U');
//1531476015
$date2=date_create_from_format('Y-m-d H:i:s T', '2018-07-13 15:30:15 IST');
echo $str2=$date2->format('U');
//1531488615
/*
$str1 IS NOT equal to $str2
but date1=date2
*/
echo $date1->format('Y-m-d H:i:s e');
//2018-07-13 15:30:15 Asia/Kolkata
echo $str3=$date1->format('U');
//1531476015
$date2=date_create_from_format('Y-m-d H:i:s e', '2018-07-13 15:30:15
Asia/Kolkata');
echo $str4=$date2->format('U');
//1531476015
/*
$str3 IS equal to $str4
as date1=date3
*/
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76624&edit=1