Skip to main content
2 of 3
added $wpdb suggestion
Pat J
  • 12.3k
  • 2
  • 28
  • 36

According to the Codex page for get_the_time(), it needs to be used in The Loop. The difference between the_time() and get_the_time() is that the former echo()es the date, and the latter returns it.

There are a couple functions that do what I think you're looking for -- get the last updated date and time for a post: get_the_modified_time() and get_the_modified_date(). It looks like they, too, need to be used in The Loop.

Here's one way to get the updated date of the most recent post in your site:

<?php
     $args = array(
        'orderby' => 'post_modified',
        'numberposts' => 1,
    );
    $myposts = get_posts( $args );
    if( have_posts() ) {
        while( have_posts() ) {
            the_post();
            $last_update = get_the_modified_date();
        }
    }
    echo( "Last modified on $last_update." );
?>

If you're sure you need to be outside of any Loops, you can always use $wpdb:

<?php
    global $wpdb;
    $sql = "SELECT post_modified
            FROM $wpdb->posts
            WHERE post_type='post'
            AND post_status='publish'
            ORDER BY post_modified DESC
            LIMIT 1";
    $last_update = $wdpb->get_var( $sql );
    echo( "Last updated $last_update." );
?>
Pat J
  • 12.3k
  • 2
  • 28
  • 36