Show Recent Post Dates as "2 Days Ago"
Displays the publish date of recent posts in a friendly relative format, such as "3 hours ago", and switches back to the normal date after a set number of days.
Opens WPIDE on
Code
// Use relative dates for posts newer than this many days.
$wpide_relative_days = 7;
add_filter( 'get_the_date', function ( $date, $format, $post ) use ( $wpide_relative_days ) {
if ( is_admin() || is_feed() || ( '' !== $format && get_option( 'date_format' ) !== $format ) ) {
return $date;
}
$timestamp = (int) get_post_time( 'U', true, $post );
if ( ! $timestamp || time() - $timestamp > $wpide_relative_days * DAY_IN_SECONDS || $timestamp > time() ) {
return $date;
}
return sprintf( '%s ago', human_time_diff( $timestamp, time() ) );
}, 10, 3 );
Notes
Machine-readable dates in datetime attributes are left untouched. Change the day limit at the top.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.