5

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!

  • 1
    I'm afraid this code will not accepted as core patch. At least not before 2030. Maybe you can try to submit a patch to add a filter to mysql2date. date_i18n has its filter that allow you to override WP output, but with it you will not able to replace result of mysql2date for G or U formats. Regarding meta box you can remove the core one and register your own that use date in your way. – gmazzap Aug 21 '14 at 17:06
  • n/p if you want an example on how to deregister default and register a custom publish metabox look at this answer – gmazzap Aug 22 '14 at 20:23
3

Hacking the core is never recommended, since you'll have to reapply your hack every time you update (and for a number of other reasons).

The first thing I would normally do is to check if there is a hook or filter to allow you to modify the date. It doesn't appear there is one.

The next thing you might check is whether the function you'd like to change is in the list of Pluggable Functions (functions that WordPress lets you override) - http://codex.wordpress.org/Pluggable_Functions - and that function doesn't appear to be.

So, my suggestion would be to create a custom date field (the Advanced Custom Fields plugin works great for that). Then if you need to sort by date, you'd just have to set your query to sort by that custom meta field rather than the standard post date field.

  • 1
    Yes, I am more than aware of the issues with hacking the core. But clearly there is no other way to do this, as you saw as well, and the sheer quantity of labor involved in making the entire site based off a second date field is unwieldy. My hope is to get this code right and submit it as a patch. It will benefit other people as well to have this date field handled properly. Thanks! – TAH Aug 21 '14 at 15:52
  • @TAH the publish date field is the wrong field to use for your purpose, so I doubt a patch to "fix" this would be welcome. the correct way to associate a date with a post is via post meta, as Justin suggests. – Milo Aug 21 '14 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.