Archived topic
Need article updated date and time in the following format
9 replies · Started by Haider Ali on February 16, 2021
Hello,
I got the article updated date shown on all articles using the following code:
add_filter( 'generate_post_date_show_updated_only', '__return_true' );
However, I would like my articles to show the time and date of it last being updated.
Example: February 17, 2021 1:35 pm UTC
For your information, UTC is the timezone.
On my previous theme, I had got it done by the following code:
function astra_add_time_with_date( $format ) {
$format = 'F j, Y g:i a T' ;
return $format;
}
Thank you
Hi there,
You can actually use the same F j, Y g:i a T format on Dasboard > Setting > General date format as shown here: https://share.getcloudapp.com/7KupojLk
Alternatively, you can filter the post date output using generate_post_date_output filter:
Example:
add_filter( 'generate_post_date_output', function() {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s %5$s</time> ';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s %6$s</time> ' . $time_string;
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date( 'F j, Y' ) ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date( 'F j, Y' ) ),
esc_html( get_the_date( 'g:i A T' ) ),
esc_html( get_the_modified_date( 'g:i A T' ) )
);
return sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
} );
Thank you Elvin.
The code you provided got the date and time displayed just like I wanted. 😊
Cheers!
No problem. Glad to be of any help. :D
Hello Elvin,
I just updated an old article and noticed that the date and time displayed is the publishing date and not the modified date/time. Could you please help with this?
Thank you 😄
Hi Haider,
Have you tried clear cache and disable cache plugin see if that works?
If not, could you link us to the post in question, you could use the private info field.
Thanks!
Hello Ying,
Yes, I have cleared cache, disabled cache plugins and still didn't see the article updated date/time.
I see the same old article date/time.
Link as has been shared in private information.
Thank you
The updated date has been generated correctly, it's this display: none;CSS is preventing it from showing :)
Got it, thank you so much Ying!
Cheers!
You are welcome :)