Using WordPress ‘get_the_modified_time()’ PHP function

The get_the_modified_time() WordPress PHP function retrieves the time at which the post was last modified.

Usage

get_the_modified_time( $format, $post )

Example:

echo 'Last modified on: ' . get_the_modified_time( 'F j, Y g:i a' );

Output:

Last modified on: May 6, 2023 2:15 pm

Parameters

  • $format (string) Optional: Format to use for retrieving the time the post was modified. Accepts ‘G’, ‘U’, or PHP date format. Defaults to the ‘time_format’ option. Default: ''
  • $post (int|WP_Post) Optional: Post ID or WP_Post object. Default current post. Default: null

More information

See WordPress Developer Resources: get_the_modified_time()

Examples

Display modified time with custom format

This example displays the modified time using a custom date format.

echo 'Last modified on: ' . get_the_modified_time( 'F j, Y' );

Display modified time with the default format

This example displays the modified time using the default ‘time_format’ option.

echo 'Last modified on: ' . get_the_modified_time();

Display modified time in Unix timestamp

This example displays the modified time as a Unix timestamp.

echo 'Last modified on: ' . get_the_modified_time( 'U' );

Display modified time for a specific post

This example displays the modified time for a specific post by ID.

$post_id = 42;
echo 'Post ' . $post_id . ' last modified on: ' . get_the_modified_time( 'F j, Y', $post_id );

Display modified time within a loop

This example displays the modified time for each post within a loop.

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        echo 'Last modified on: ' . get_the_modified_time( 'F j, Y g:i a' ) . '<br>';
    }
}