Display last date a post was updated on WordPress

I’m using some PHP to display the last time a blog post was updated on WordPress using the get_the_time and get_the_modified_time functions. However, I can’t get the last modified time to display inline in the paragraph.

 <?php 
        $x = get_the_time('U');             
        $m = get_the_modified_time('U');    
        if ($m != $x) { 
            $t = the_modified_time('F d, Y');
            echo "<p class="lastupdated">Updated on $t </p>"; 
        } 
    ?>

Here’s a screenshot of the result:

Read More

Screenshot

Related posts

Leave a Reply

5 comments

  1.      <?php 
                $x = get_the_time('U');             
                $m = get_the_modified_time('U');    
                if ($m != $x) { 
                    echo "<p class="lastupdated">Updated on ".the_modified_time('F d, Y')."</p>"; 
                } 
            ?>
    
  2. You can also display the last modified time of a post through this code by placing it anywhere in your loop.

    Posted on <?php the_time('F jS, Y') ?>
    <?php $u_time = get_the_time('U');
    $u_modified_time = get_the_modified_time('U');
    if($u_modified_time != $u_time) {
        echo "and last modified on ";
        the_modified_time('F jS, Y');
        echo ". ";
    } ?>
    
  3. This is the complete code based on the above comments. Now it works and you just need to add this to your post template:
    Snippet (1)

    <?php 
            $x = get_the_time('U');             
            $m = get_the_modified_time('U');    
            if ($m != $x) { 
                $t = get_the_modified_time('F d, Y');
                echo "<p class="lastupdated">Updated on $t </p>"; 
            } 
        ?>
    

    For example, suppose that your post template is called content-post.php. Look for a part that looks like the below:
    Snippet (2)

                // Post date
                if ( in_array( 'post-date', $post_meta_top ) ) : ?>
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_time( get_option( 'date_format' ) ); ?></a>
                <?php endif;
    

    Insert snippet (2) immediately before the closing tag of snippet (1) as follows:

            <?php
    
            // Post date
            if ( in_array( 'post-date', $post_meta_top ) ) : ?>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_time( get_option( 'date_format' ) ); ?></a>
    
            <?php 
                $x = get_the_time('U');             
            $m = get_the_modified_time('U');    
            if ($m != $x) { 
            $t = get_the_modified_time('F d, Y');
            echo "<p class="lastupdated">Updated on $t </p>"; 
                } 
                ?>
    
    
            <?php endif;
    

    Now, you will get the original post date and the updated post date. Special thanks go to @Sebastian Paaske Tørholm for his comment.