Req #78504 [NEW]: DateTime can quietly produce unexpected results

From: Date: Fri, 06 Sep 2019 08:55:39 +0000
Subject: Req #78504 [NEW]: DateTime can quietly produce unexpected results
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222597@lists.php.net to get a copy of this message
From: slawek1211 at gmail dot com Operating system: All OS'es (documented) PHP version: 7.1.32 Package: Date/time related Bug Type: Feature/Change Request Bug description:DateTime can quietly produce unexpected results Description: ------------ Hi Guys, i think there's a small issue with how DateTime object works and it can be made much better with just a very small change. So now when you're using timestamp in constructor or in DateTime::CreateFromFormat then php silently ommits specified timezone. Same thing happens when you include timezone in the time itself, and also specify it - then timezone contained in time string has preference. This is documented, but very counter-intuitive. So in case where the object gets created with different timezone than specified in $timezone parameter it would be great to include a warning. Test script: --------------- // Example 1 $tz = new DateTimeZone('America/New_York'); $time_o = new DateTime('@'.time(), $tz); var_dump($time_o->format("Y-m-d H:i:s")); echo "<br>"; // Example 2 $time_o = DateTime::CreateFromFormat("U", time(), $tz); var_dump($time_o->format("Y-m-d H:i:s")); Expected result: ---------------- Add 2 warnings, depending on case. 1. Warning: DateTime object created from timestamp, timezone forced to UTC (ignoring America/New_York) in line 2 2. Warning: CreateFromFormat has timezone specified both in $time and in $timezone, ignoring $timezone in line 1 Actual result: -------------- PHP is silently ignoring optional function parameter specified by user. Which is counter intuitive, because if someone specifies time and timezone he's probably counting on that time to be presented in timezone given, despite the format. Other action which would make it better is to always honor $timezone first. Because many people find this confusing that the parameter gets ignored in DateTime documentation. But this could break existing code and probably is much harder to implement so just a warning would be great... -- Edit bug report at https://bugs.php.net/bug.php?id=78504&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=78504&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=78504&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=78504&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=78504&r=needscript Try newer version: https://bugs.php.net/fix.php?id=78504&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=78504&r=support Expected behavior: https://bugs.php.net/fix.php?id=78504&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=78504&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=78504&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=78504&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=78504&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=78504&r=dst IIS Stability: https://bugs.php.net/fix.php?id=78504&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=78504&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=78504&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=78504&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=78504&r=mysqlcfg

« previous php.bugs (#222597) next »