Opened 12 years ago
Closed 6 years ago
#25002 closed defect (bug) (fixed)
get_post_time() doesn't return correct GMT time if date_default_timezone_set is changed from UTC.
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 5.3 | Priority: | normal |
| Severity: | normal | Version: | 1.2 |
| Component: | Date/Time | Keywords: | has-unit-tests has-patch |
| Focuses: | Cc: |
Description
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.
More information here: http://weston.ruter.net/2013/04/02/do-not-change-the-default-timezone-from-utc-in-wordpress/
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.
Attachments (3)
Change History (12)
#7
@
7 years ago
- Keywords bulk-reopened added
Ok, so this touches on a lot of moving parts. I am generally proofing core against sensitivity to default time zone (see #44491 ) so those parts are getting slowly washed out.
I am looking at fixing up mysql2date (related #28992 ), but essentially it's garbage in garbage out. It was originally written to work with ambiguous timezone-less inputs and it accepts pretty much whatever, so it borders on unrealistic to clean it up into something sane.
For posts my current plan is to introduce a new get_post_datetime() function, that will instance a proper timezone-aware DateTime object from a post. Using that will knock out a lot of issues with how post times are read and output.
#8
@
6 years ago
- Keywords has-unit-tests has-patch added; needs-patch needs-testing bulk-reopened removed
Ok, mysql2date() is getting fixed up some in #28992, but get_post_time() (surprise! surprise!) is broken anyway.
Really not keen on keeping using mysql2date() for GMT times, doesn't really make sense.
As planned added patch introducing get_post_datetime(), changing older functions to use it, with unit tests for old quirky logic.
Patch to fix mysql2date issue.