Using WordPress ‘get_the_modified_time’ PHP filter

The get_the_modified_time WordPress PHP filter allows you to modify the localized time a post was last edited.

Usage

add_filter('get_the_modified_time', 'your_custom_function', 10, 3);
function your_custom_function($the_time, $format, $post) {
    // your custom code here
    return $the_time;
}

Parameters

  • $the_time (string|int|false) – The formatted time or false if no post is found.
  • $format (string) – Format to use for retrieving the time the post was modified. Accepts ‘G’, ‘U’, or PHP date format.
  • $post (WP_Post|null) – WP_Post object or null if no post is found.

More information

See WordPress Developer Resources: get_the_modified_time

Examples

Change date format

Modify the date format of the modified time to ‘F j, Y’.

add_filter('get_the_modified_time', 'change_modified_time_format', 10, 3);
function change_modified_time_format($the_time, $format, $post) {
    $new_format = 'F j, Y';
    return get_post_modified_time($new_format, false, $post, true);
}

Append custom text

Add custom text before the modified time.

add_filter('get_the_modified_time', 'append_custom_text', 10, 3);
function append_custom_text($the_time, $format, $post) {
    return 'Last updated: ' . $the_time;
}

Hide modified time for specific category

Do not display modified time for posts in the ‘news’ category.

add_filter('get_the_modified_time', 'hide_modified_time_for_news', 10, 3);
function hide_modified_time_for_news($the_time, $format, $post) {
    if (in_category('news', $post)) {
        return '';
    }
    return $the_time;
}

Display relative time

Show modified time as a relative time (e.g., “5 minutes ago”).

add_filter('get_the_modified_time', 'display_relative_modified_time', 10, 3);
function display_relative_modified_time($the_time, $format, $post) {
    $time_diff = human_time_diff(get_post_modified_time('U', true, $post), current_time('timestamp'));
    return $time_diff . ' ago';
}

Show modified time in different timezone

Display modified time in a specific timezone, e.g., New York (America/New_York).

add_filter('get_the_modified_time', 'change_modified_time_timezone', 10, 3);
function change_modified_time_timezone($the_time, $format, $post) {
    $timestamp = get_post_modified_time('U', true, $post);
    $date_obj = new DateTime("@$timestamp");
    $date_obj->setTimeZone(new DateTimeZone('America/New_York'));
    return $date_obj->format('F j, Y g:i a');
}