I'm using Wordpress 3.9.2, MySQL 5.1.73, & PHP 5.3.27 to put together a history-based site.
As is well-documented elsewhere (including many questions on StackExchange & WP forums over the years), due to the limitations of PHP, one cannot have posts with dates outside of the minimum and maximum values for a 32-bit signed integer. As I am running on a 64-bit system, I researched the problem further and found this useful research elsewhere on StackExchange.
Using that information, I was able to put together a solution with surprisingly little code to overcome what basically boils down a PHP limitation w/ strtotime()
(but not the MySQL limitation of years in the 1000-9999 range). I checked the following places to confirm that the date is treated properly: post creation/update in the admin, date on the post in the front-end and admin sites, sorting of posts on list pages, and date in RSS feeds.
Although I have years of programming experience, PHP and Wordpress are not my forte, so I'm posting this for folks with that expertise to address 2-3 problems with my proposed solution.
- Couldn't see a way to do this w/o hacking the core.
- Don't know how to handle date localization (noted in the below).
- Not sure if I tested everywhere necessary.
But here is the solution I came up with:
(1) Replace mysql2date in wp-includes/functions.php:
function mysql2date( $format, $date, $translate = true ) {
if ( empty( $date ) )
return false;
if ( 'G' == $format )
try {
$i = new DateTime($date . ' +0000');
return $i->format('G');
} catch (Exception $e) { // This doesn't appear to be WP's way of handling errors
echo $e->getMessage();
exit(1);
}
try {
$i = new DateTime($date);
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
if ( 'U' == $format )
return $i->format('U');
if ( $translate )
return $i->format($format); // Not sure how to localize here (had date_i18n before)
else
return $i->format($format);
}
(2) Edit post_submit_meta_box() in wp-admin/includes/meta-boxes.php -- replace line 175 with the following:
try {
$date = new DateTime($post->post_date);
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
$date = $date->format($datef); // Same issue as before -- this date should be localized
Thanks in advance for showing a better way to implement these fixes (or pointing out where these fixes fail).
Update
I know I know don't hack the core. I would like to submit this code as a patch to Wordpress, hence my desire to be sure I've done it the right way. Thank you!
mysql2date
.date_i18n
has its filter that allow you to override WP output, but with it you will not able to replace result ofmysql2date
forG
orU
formats. Regarding meta box you can remove the core one and register your own that use date in your way.