Make WordPress Core

Opened 6 years ago

Last modified 6 weeks ago

#48936 assigned enhancement

Remove mysql2date() usage from core

Reported by: rarst's profile Rarst Owned by: pbearne's profile pbearne
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Date/Time Keywords: needs-patch needs-unit-tests
Focuses: Cc:

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 (4)

#1 @Rarst
6 years ago

  • Type changed from defect (bug) to enhancement

#2 @coquardcyr
2 years ago

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

  • Line 150 of src/wp-content/themes/twentytwenty/classes/class-twentytwenty-walker-page.php
  • 346 of src/wp-admin/includes/class-wp-ms-users-list-table.php
  • Lines 248, 627 and 630 of src/wp-admin/edit-form-advanced.php
  • Lines 103 and 104 from src/wp-includes/feed-atom-comments.php
  • Line 79 from src/wp-includes/feed-rdf.php
  • Line 96 from src/wp-includes/feed-rss2.php
  • Line 96 src/wp-includes/feed-rss2-comments.php
  • Line 220 of src/wp-admin/edit-form-blocks.php
  • Lines 326 to 331 of src/wp-admin/includes/template.php
  • Lines 469, 470 and 477 of src/wp-includes/class-wp.php
  • Line 217 of src/wp-includes/class-walker-page.php
  • Lines 479 and 503 of src/wp-admin/includes/class-wp-ms-sites-list-table.php
  • Line 583 of src/wp-admin/includes/export.php
  • Lines 538 and 551 of src/wp-includes/ms-functions.php
  • Lines 2257 and 2850 src/wp-admin/includes/ajax-actions.php
  • Lines 2673 and 2689 of src/wp-includes/class-wp-customize-manager.php
  • Lines 590 and 1086 of src/wp-includes/comment-template.php
  • Line 1813 of src/wp-includes/class-wp-editor.php
  • Lines 900 and 901 of src/wp-includes/comment.php
  • Lines 880 and 892 of src/wp-includes/class-wp-xmlrpc-server.php
  • Lines 4717 and 4718 of src/wp-includes/class-wp-query.php
  • Lines 2049 and 2328 of src/wp-includes/link-template.php
  • Lines 2119 and 2500 of src/wp-includes/general-template.php
  • Line 1695 of src/wp-admin/includes/media.php

-Line 1274 of src/wp-admin/includes/upgrade.php
-Line 7418 of src/wp-includes/functions.php

Version 0, edited 2 years ago by coquardcyr (next)

#3 @pbearne
2 months ago

  • Owner set to pbearne
  • Status changed from new to assigned

What should we replace this with?

#4 @sukhendu2002
6 weeks 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.

Note: See TracTickets for help on using tickets.