4

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.

6
  • What do you mean by actually modified? That it has been edited at least once after publishing?
    – N00b
    May 10, 2016 at 17:48
  • Now when I publish my post, I get the time and the modified time, but the post itself is not modified, it is only published, it hasn't been updated with fresh or new content. So I need some if statement or correcting statement I provided up there, so wp can check if it has been updated before it displays modified time. So the modified time will be displayed only if post is edited after publishing.
    – Usce
    May 10, 2016 at 17:51
  • According to the code you posted, nothing will display if the times match. Which, they will when a post is initially published. WP sets the publish and updated time to the same value when a post is first created.
    – darrinb
    May 10, 2016 at 17:54
  • So what you suggest, should I remove ! not comparator, but I don't understand how wordpress sets the same time for both values, because the get_the_modified_time function should display time when post is updated or? @N00b I will try this
    – Usce
    May 10, 2016 at 17:57
  • Try get_the_modified_time() > get_the_time() instead. You might want to echo both in order to check them, because as far as i know, both have to be in same date-time format, otherwise you can't compare them.
    – N00b
    May 10, 2016 at 17:57

1 Answer 1

8

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.

1
  • This works, thank you very much, also the only edit I made here is set the Y-m-d in get_the_modified_time(). Also didn't know about Unix timestamp, so thank you for sharing this :)
    – Usce
    May 10, 2016 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.