WordPress “Old Dates” Historical Timestamp Function

UPDATE: This function is now available as a WordPress plugin. The original function is discussed below and is also available for download on Studio Hyperset‘s Google code project site.

WordPress developers often find themselves frustrated by historical “old” dates, that is, pre-1970 or, in some cases, pre-1969 post and page timestamps. Usually, when developers try to assign such dates to posts, the system ignores them and outputs the current date instead.

Here’s a list of relevant discussions from the WordPress forum:

Various fixes and workarounds have been proposed in these posts and on the WordPress Trac. And while this particular fix may not work in all server environments — the problem being, at its core, a Unix/PHP limitation — for most developers, it’s likely the easiest and, since it involves no changes to WordPress core files, least obtrusive way to integrate pre-1970/pre-1969 historical, old date timestamps into WordPress posts and pages.

USING THE FUNCTION

In your theme index.php (and/or single.php) file, find any standard WordPress <?php the_time(); ?> or <?php the_date(); ?> function inside the loop. Depending on desired output, characters between the parentheses may vary. For example, a common timestamp function would read <?php the_time('F jS, Y');?> which, if the post/page were published on 1/11/11, would output "January 1st, 2011."

For more date customization options, see the WordPress Codex and the PHP manual. Moreover, while the following function works with both the_time(); and the_date();, for more information on the proper use of each, see the functions’ respective entries in the WordPress Codex:

If you want to adjust this timestamp so WordPress outputs a pre-1960/pre-1970 historical, old date year, delete the year portion of the_time(); and/or the_date(); function — e.g., " … Y);" — and replace it with the following:

');
$date = get_the_date();
$newdate = strtotime ( '-150 year' , strtotime ( $date ) );
$newdate = date ( 'Y' , $newdate );
echo $newdate;

In this case, "-150 year" is the differential, and it may be adjusted as needed.

For example, a post or page with a publication date of 1/11/11 — that is, <?php the_time('F jS, Y'); ?> or "January 1st, 2011" — would become:

<?php the_time('F jS, ');
$date = get_the_date();
$newdate = strtotime ( '-150 year' , strtotime ( $date ) );
$newdate = date ( 'Y' , $newdate );
echo $newdate; ?>

… which would output “January 1st, 1861.”

Be sure to set your preferred post or page month and day within WordPress’ admin framework. The function will only adjust the year, based on whatever common differential you provide. In the above example, this would be -150 years. This means you’ll want to set your post/page year as many years in the future as required by the differential. For example, again using the above function, if I want a page or post to display a historical old date of 1861, and my common function differential is 150 years, I would give that page or post a publication date of 2011.

Even when using plugins like shacker and rboren‘s “The Future is Now,” some WordPress developers have reported problems with dates beyond 2038. (Strictly speaking, this is another Unix issue and not a WordPress limitation per se.) And, of course, you can only assign publication dates as far back as 1970 or, perhaps, 1969.

As such, if a developer has posts or pages with historical old dates that differ by many hundreds of years, s/he may need to use multiple instances of the function with multiple unique differentials.

Using WordPress conditionals, developers can easily integrate multiple versions of the function into their theme to correspond with different posts and pages and can thereby employ larger or smaller differentials as required.

To see the function in action, visit SH‘s Flâneur journal: an asset of its script with the same name.

Posted in "Métier ."

Tags: