Skip to content

Instantly share code, notes, and snippets.

@phlbnks
Last active February 3, 2020 10:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phlbnks/4a48123b9e6a1299b3f17ff521885137 to your computer and use it in GitHub Desktop.
Save phlbnks/4a48123b9e6a1299b3f17ff521885137 to your computer and use it in GitHub Desktop.
Filter WordPress date functions to return relative time strings.
/**
* Filter the_time and get_the_date to return a relative string.
* Inspired by http://www.jasonbobich.com/2011/04/10/a-better-way-to-add-time-ago-to-your-wordpress-theme/
* Based on code from https://buddypress.trac.wordpress.org/browser/tags/2.8.2/src/bp-core/bp-core-functions.php#L1118
*
* @param string $formatted The formatted time.
* @param string $format The time format used.
* @param object $the_post Optional - current Post object.
* @return string Modified formatted time.
*/
function cc_relative_time( $formatted, $format, $the_post = false ) {
// Certain formats are likely to mean use in <time> elements and similar so don't touch them.
$timestamp_formats = array(
'c', // ISO 8601.
'r', // RFC 2822.
'U', // Unix timestamp.
);
if ( in_array( $format, $timestamp_formats, true ) ) {
return $formatted;
}
// Get the current post object from the global if not supplied.
if ( ! $the_post ) {
global $post;
$the_post = $post;
}
// Get the date/time for the post in GMT.
$post_date = get_post_time( 'U', true, $the_post );
// Get the current data/time in GMT for comparrison.
$current_date = current_time( 'timestamp', true );
// Language vars.
$unknown_text = __( 'sometime', 'customcreative' );
$right_now_text = __( 'right now', 'customcreative' );
$ago_text = __( '%s ago', 'customcreative' );
// Array of time period chunks.
$chunks = array(
YEAR_IN_SECONDS,
30 * DAY_IN_SECONDS,
WEEK_IN_SECONDS,
DAY_IN_SECONDS,
HOUR_IN_SECONDS,
MINUTE_IN_SECONDS,
1,
);
// Difference in seconds between curent time and post date/time.
$since = $current_date - $post_date;
// Something went wrong with date calculation and we ended up with a negative date.
if ( 0 > $since ) {
return $formatted;
}
// Find the relevant time chunk.
for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) {
$seconds = $chunks[ $i ];
// Finding the biggest chunk (if the chunk fits, break).
$count = intval( $since / $seconds );
if ( 0 !== $count ) {
break;
}
}
// If $i iterates all the way to $j, then the event happened 0 seconds ago.
if ( ! isset( $chunks[ $i ] ) ) {
$output = $right_now_text;
} else {
// Set output var based on $chunk.
switch ( $seconds ) {
case YEAR_IN_SECONDS :
$output = sprintf( _n( '%s year ago', '%s years ago', $count, 'customcreative' ), $count );
break;
case 30 * DAY_IN_SECONDS :
$output = sprintf( _n( '%s month ago', '%s months ago', $count, 'customcreative' ), $count );
break;
case WEEK_IN_SECONDS :
$output = sprintf( _n( '%s week ago', '%s weeks ago', $count, 'customcreative' ), $count );
break;
case DAY_IN_SECONDS :
$output = sprintf( _n( '%s day ago', '%s days ago', $count, 'customcreative' ), $count );
break;
case HOUR_IN_SECONDS :
$output = sprintf( _n( '%s hour ago', '%s hours ago', $count, 'customcreative' ), $count );
break;
case MINUTE_IN_SECONDS :
$output = sprintf( _n( '%s minute ago', '%s minutes ago', $count, 'customcreative' ), $count );
break;
default:
$output = sprintf( _n( '%s second ago', '%s seconds ago', $count, 'customcreative' ), $count );
}
}
return $output;
}
add_filter( 'the_time', 'cc_relative_time', 10, 2 );
add_filter( 'get_the_time', 'cc_relative_time', 10, 3 );
add_filter( 'the_date', 'cc_relative_time', 10, 2 );
add_filter( 'get_the_date', 'cc_relative_time', 10, 3 );
@phlbnks
Copy link
Author

phlbnks commented Apr 25, 2017

Wrap into a plugin or place in functions.php

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