I want to display post updated time only if post is modified, I am using function the_modified_time() . So I just read about that there is if statement that can be used to check if post is even modified that looks like this:

if (get_the_modified_time() != get_the_time())

So I put this in my template where the time itself is being displayed so it looks like this now:

 if (get_the_modified_time() != get_the_time()){
    echo 'Last updated:' . the_modified_time();

}

And it is not showing at all. So obviously I am missing something here.

1 Answer
1

Loosely what you have should work already. However few things are off.

  1. Calling these function without time format will produce values like 1:36 pm (depending on your site’s settings), which are not exactly comparable.
  2. Post modified time can be less than published in some cases, like scheduled posts.

So I would write it along the lines of:

if ( get_the_modified_time( 'U' ) > get_the_time( 'U' ) ) {
    echo 'Last updated:' . get_the_modified_time();
}

U format stands for a numeric Unix timestamp, which is comparison–friendly.

Leave a Reply

Your email address will not be published. Required fields are marked *