Make WordPress Core

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)

#1 @Rarst
7 years ago

  • Type defect (bug) → enhancement

#2 @coquardcyr
4 years ago

Inside the core, we have the following usages from the function:

Last edited 19 months ago by sabernhardt (previous) (diff)

#3 @pbearne
19 months ago

  • Owner set to pbearne
  • Status new → assigned

What should we replace this with?

#4 @sukhendu2002
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() and get_comment_time() parse dates directly, the same way get_post_time() does. With $gmt = true, a format with timezone characters now outputs +00:00 instead 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() builds Last-Modified from the raw GMT values with gmdate().
  • XML-RPC _convert_date(), _convert_date_gmt() and the if_not_modified_since check in wp_getPosts.
  • Customizer changeset scheduling, the autosave checks on the post edit screens, and the signup expiry checks in ms-functions.php use strtotime().

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.php calls. They treat UTC as local today, and fixing that changes output.
  • deprecated.php and the bundled themes.
  • Deprecating mysql2date() itself.

Notes for reviewers:

  • Feeds and export now pass the final format to the get_post_time / get_comment_time filters instead of 'Y-m-d H:i:s'. feed-rdf.php now runs the get_post_time filter.
  • 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.php
  • tests/phpunit/tests/date/xmlrpc.php
  • tests/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:

  1. Set Settings > General > Timezone to a non-UTC city, for example Helsinki.
  2. Load /feed/, /feed/rdf/ and /comments/feed/. The pubDate / dc:date / updated values should match the GMT columns in the database, and the Last-Modified response header should be in GMT.
  3. Run Tools > Export and check the pubDate values in the file.
  4. get_comment_time( 'c', true ) should end in +00:00.

#6 @faisalahammad
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 @pbearne
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 @faisalahammad
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.

Note: See TracTickets for help on using tickets.