Opened 7 years ago
Last modified 2 weeks ago
#48936 assigned enhancement
Remove mysql2date() usage from core
| Reported by: | Rarst | Owned by: | pbearne |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Date/Time | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
mysql2date() was originally meant from processing times as stored by WP in database.
Unfortunately its design is very limited because both time zone of input and output is ambiguous. It is interchangeably used for local and UTC times.
As implementation detail that meant that it would produce incorrect output for formats including time zones for local time inputs. Time zone would happen be UTC from WP core setting PHP time zone to UTC on load.
In 5.3 release this behavior was flipped to produce correct local time output for local time input, as considerably more common. Accordingly now UTC input produces incorrect output for formats with time zone.
While the function is common and familiar, it is hardly irreplaceable and its design is so limited it seems to be unsalvageable.
I propose we move towards eliminating its use in core and eventually formally deprecating it.
Change History (8)
#4
@
19 months ago
I think we can use wp_date() for most cases. It handles timezones properly.
For displaying dates to users, we'd do something like:
$datetime = date_create($date_string, wp_timezone()); wp_date($format, $datetime->getTimestamp());
For UTC time outputs (like in feeds), gmdate() works well:
$timestamp = strtotime($date_string); gmdate($format, $timestamp);
For comparing dates, it's cleaner to just create DateTime objects and compare them directly rather than converting to timestamps.
This ticket was mentioned in PR #14467 on WordPress/wordpress-develop by @faisalahammad.
4 weeks ago
#5
- Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
https://core.trac.wordpress.org/ticket/48936
What and why
mysql2date() always parses its input in the site timezone. When it is given a UTC value and a format that prints a timezone, the output shows the site offset instead of UTC. Core already has better tools for each case: get_post_time(), wp_date(), get_gmt_from_date(), and plain strtotime() for comparisons. This PR replaces the first batch of mysql2date() calls with those, and fixes get_comment_time() so $gmt = true reads the stored date as UTC.
Scope
In scope (first pass):
get_comment_date()andget_comment_time()parse dates directly, the same wayget_post_time()does. With$gmt = true, a format with timezone characters now outputs+00:00instead of the site offset.- The RSS2, RDF, RSS2 comments and Atom comments feeds, and the WXR export, pass their final format straight to
get_post_time()/get_comment_time(). WP::send_headers()buildsLast-Modifiedfrom the raw GMT values withgmdate().- XML-RPC
_convert_date(),_convert_date_gmt()and theif_not_modified_sincecheck inwp_getPosts. - Customizer changeset scheduling, the autosave checks on the post edit screens, and the signup expiry checks in
ms-functions.phpusestrtotime().
Output from all touched callers is unchanged, except for the get_comment_time() GMT fix above.
Out of scope, for follow-up patches:
- Admin display calls that format local time.
- List table and
upgrade.phpcalls. They treat UTC as local today, and fixing that changes output. deprecated.phpand the bundled themes.- Deprecating
mysql2date()itself.
Notes for reviewers:
- Feeds and export now pass the final format to the
get_post_time/get_comment_timefilters instead of'Y-m-d H:i:s'.feed-rdf.phpnow runs theget_post_timefilter. - UTC values that fall inside a site timezone DST gap are no longer shifted by an hour.
Testing
New tests, all tagged @ticket 48936:
tests/phpunit/tests/date/getCommentDate.phptests/phpunit/tests/date/xmlrpc.phptests/phpunit/tests/wp/sendHeaders.php
npm run env:start npm run env:install npm run test:php -- --group 48936 npm run test:php -- --group date,datetime,comment,feed,xmlrpc,customize,wp,export npm run test:php -- -c tests/phpunit/multisite.xml --filter signup
On trunk, only the GMT time zone test fails. The other new tests pass before and after, which confirms the output did not change.
Manual check:
- Set Settings > General > Timezone to a non-UTC city, for example Helsinki.
- Load
/feed/,/feed/rdf/and/comments/feed/. ThepubDate/dc:date/updatedvalues should match the GMT columns in the database, and theLast-Modifiedresponse header should be in GMT. - Run Tools > Export and check the
pubDatevalues in the file. get_comment_time( 'c', true )should end in+00:00.
#6
@
4 weeks ago
I've opened a PR for a first pass at this: https://github.com/WordPress/wordpress-develop/pull/14467
It replaces mysql2date() in the comment template tags, feeds, export, WP::send_headers(), XML-RPC, Customizer scheduling, autosave checks and signup expiry. No new API, and output should stay the same.
One behavior change to flag: get_comment_time() with $gmt = true was parsing the GMT date in the site timezone, so formats with timezone characters (c, r, T, O) showed the site offset instead of UTC. That's fixed here, with tests.
Out of scope for now: mysql2date() itself and the remaining calls in get_the_date(), get_the_modified_date() and friends, which need more discussion around the $translate behavior.
Reviews welcome.
#7
@
4 weeks ago
@faisalahammad
Thank you for the patch I will look at it
I suspect that we need to deprecate mysql2date() as part of the patch
So feel free to keep working on this
#8
@
2 weeks ago
Thanks @pbearne. Agreed that deprecation is the end goal. Doing it in this patch would mean replacing the remaining 37 calls first (admin screens, multisite list tables, upgrade.php, the comment flood check, Walker_Page, Twenty Twenty), otherwise core itself would trigger the notice. Some of those change output today, since the list tables and upgrade.php treat UTC values as local time, so they deserve their own review.
I’d suggest landing #14467 as phase 1, then a phase 2 PR that replaces the remaining calls and deprecates mysql2date() in the same commit. Happy to put that up next. If you’d rather have it all in one PR, I can extend this one instead.
Inside the core, we have the following usages from the function:
src/wp-content/themes/twentytwenty/classes/class-twentytwenty-walker-page.phpsrc/wp-admin/includes/class-wp-ms-users-list-table.phpsrc/wp-admin/edit-form-advanced.phpsrc/wp-includes/feed-atom-comments.phpsrc/wp-includes/feed-rdf.phpsrc/wp-includes/feed-rss2.phpsrc/wp-includes/feed-rss2-comments.phpsrc/wp-admin/edit-form-blocks.phpsrc/wp-admin/includes/template.phpsrc/wp-includes/class-wp.phpsrc/wp-includes/class-walker-page.phpsrc/wp-admin/includes/class-wp-ms-sites-list-table.phpsrc/wp-admin/includes/export.phpsrc/wp-includes/ms-functions.phpsrc/wp-admin/includes/ajax-actions.phpsrc/wp-includes/class-wp-customize-manager.phpsrc/wp-includes/comment-template.phpsrc/wp-includes/class-wp-editor.phpsrc/wp-includes/comment.phpsrc/wp-includes/class-wp-xmlrpc-server.phpsrc/wp-includes/class-wp-query.phpsrc/wp-includes/link-template.phpsrc/wp-includes/general-template.phpsrc/wp-admin/includes/media.phpsrc/wp-admin/includes/upgrade.phpsrc/wp-includes/functions.phpsrc/wp-includes/media.php