Skip to content

Instantly share code, notes, and snippets.

@phlbnks
Last active May 20, 2018 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phlbnks/d71101eee4d33678e3e4a8c44adf1dbe to your computer and use it in GitHub Desktop.
Save phlbnks/d71101eee4d33678e3e4a8c44adf1dbe to your computer and use it in GitHub Desktop.
Filter the *displayed* time of posts in WordPress to move them into the past/future.
/**
* Filter the_time function to show posts as being from another date/time.
*
* @param string $formatted The formatted time.
* @param string $format The time format used.
* @return string Modified formatted time.
*/
function cc_time_machine( $formatted, $format ) {
$offset = get_option( 'cc_time_offset' ); // Offset in days, can be negative.
if ( $offset ) {
global $post;
$timestamp = get_post_time( 'U', true, $post ); // UTC timestamp with GMT offset included.
$adjusted = $timestamp + ( (int) $offset * DAY_IN_SECONDS );
return date( $format, $adjusted );
}
// If option doesn't exist, then just return it unmodified.
return $formatted;
}
add_filter( 'the_time', 'cc_time_machine', 10, 2 );
@phlbnks
Copy link
Author

phlbnks commented Apr 25, 2017

Wrap this into a plugin or just drop it into your functions.php
Remember you need to create / manage the option for $offset, or remove get_option() and replace it with a value, e.g. '-15'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment