get_post_time() doesn't return correct GMT time if date_default_timezone_set is changed from UTC.
|Reported by:||johnregan3||Owned by:|
get_post_time() and get_post_modified_time() have a parameter for GMT(UTC) time. However, they use mysqldate(), which does not take such a parameter (i.e., mysql2date expects the default time zone to be UTC).
mysql2date() uses strtotime() or date(), both of which return values relative to the default time zone (date_default_timezone_set()). So, if a developer changes the default timezone and then requests a GMT date from get_post_time() or get_post_modified_time(), they will receive an erroneous return value.
To reproduce the problem, run the enclosed unit test. It requests a GMT time from the function twice: once while in GMT, and once in another timezone (America/Chicago). The two results should be identical because they both request GMT time, but they are not.
Just to confirm, this problem occurs when all plugins are deactivated and the default theme is used.
I am submitting two files with this ticket:
- A Unit Test (mysql2date.php) displaying the faults in mysql2date (and showing that the new patch fixes the issue).
- A Patch to fix the bug. This patch has been tested using the Unit Test above.
The patch begins by adding a fourth optional parameter to mysql2date, $gmt, which will tell the function that it needs to return a GMT value. This $gmt variable will be sent from get_post_time() and get_post_modified_time(). Then, if GMT is requested, the date_default_timezone_set is set to UTC (WP's default timezone), then conditional statements instruct mysql2date as to what value to return. There is more documentation within the patch itself.