Question

In my theme, I'd like to display the publish date and (only) if the post was updated after the publish date, also display the updated date. The code in my theme now looks like this:

*/ stuff above here to display publish date, then do this stuff to see if updated */
if ( get_the_date() != get_the_modified_date() )
    {
        printf( __( '<br>Updated: <time class="entry-date" datetime="%1$s">%2$s</time>', 'splatone' ),
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() )
    );
    }

If the post was published and updated on the same day, I'd rather not show the updated line, but it currently is. How can I compare the dates so that I only get it if the date has changed and not just the time?

Was it helpful?

Solution

get_the_date() and get_modified_date() don't return time values: so, if you change your conditional statement to the below, it should work:

if ( get_the_modified_date() > get_the_date() )
{
...
}

Now if the updated date is greater than the original publish date(by one day), the if statement is true.

OTHER TIPS

This worked for me, it may help any one that Google search leads here. Please note that the time will be the same if the post is not updated but different if the post is updated.

if ($post->post_type == 'post' && get_the_date('U') !== get_the_modified_date('U')) {

// do stuff on post modified

}

If post is not updated after published, time will be the same

  if ($post->post_type == 'post' && get_the_date('U') === get_the_modified_date('U')) {
    
    // do stuff on post published only (not modified)
    
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top