|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-07-14 20:39 UTC] 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
*/
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 14 20:00:01 2025 UTC |
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=0What 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";