Opened 6 years ago
Last modified 6 years ago
#48740 new enhancement
Add constant for database date format
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Date/Time | Keywords: | needs-patch dev-feedback |
| Focuses: | Cc: |
Description
A lot of core Date/Time code is dealing with Y-m-d H:i:s format, as stored for posts and other objects in database.
This is effectively a so-called "magic" string, which is not self-explanatory and prone to user error if typed by hand (I messed up i with m for minutes more than once probably).
Since there is no upstream PHP constant for this format, I suggest we introduce one in core and use it in place of magic string:
define( 'WP_DATE_MYSQL', 'Y-m-d H:i:s' );
Not absolutely sure about the name, I used DATE_MYSQL as class constant before, but WP_DATE_DATABASE might be more appropriately generic. To my knowledge there isn't a specific ISO or RFC designation for such format.
If there is agreement on the need and the name I'll work on a patch for replacement and switch from strings in core.
I have developed custom WordPress solutions that used
DATEandDATETIME(6)types in custom database tables. I don't think WordPress is using these MySQL types currently. But the nameWP_DATE_MYSQLcould be confusing, for me it makes more sense that that constant contains the'Y-m-d'notation. Could be improved by using something like this:define( 'WP_DATE_MYSQL_DATE', 'Y-m-d' );define( 'WP_DATE_MYSQL_DATETIME', 'Y-m-d H:i:s' );define( 'WP_DATE_MYSQL_DATETIME_6', 'Y-m-d H:i:s.u' );Maybe other database systems do accept a ISO 8601 date in queries?