# 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.

- Type: PHP
- Runs: Frontend only
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: dates, posts, display
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/relative-post-dates/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Show Recent Post Dates as "2 Days Ago"" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
