Archived topic
Date difference feature
12 replies · Started by Raul on April 10, 2022
Hi
Just a suggestion / feature request:
I'm thinking that for the dynamic data, it would be useful if you implement a feature: option to show the time difference, instead of the post's published date.
So, instead of: April 1, 2022 to show something like "10 days ago"; "2 weeks ago", "6 months ago" etc.
Sorry, not for posts but for comments. I was thinking about my custom post types, in fact.
Hi there,
Are you referring to the comments posted date?
If so the comment system is entirely handled by WordPress itself.
Ah, ok. Isn't possible to have a php function to handle that?
Hi Raul,
Yes, there is a Filter Hook for comments. Specifically, get_comment_date.
To format it similar to your preference, you can add a PHP code like this through a plugin like Code Snippets:
add_filter( 'get_comment_date', 'modify_comment_date' );
function modify_comment_date( $the_date ) {
$time = human_time_diff( get_the_time('U'), current_time('timestamp') );
$updated = human_time_diff( get_the_modified_time('U'), current_time('timestamp') );
return sprintf( '%1$s ago. Updated %2$s',
$time,
$updated
);
}
Here is how it looks on my test website: https://share.getcloudapp.com/lluE07j9
Hope this helps. :)
This is excellent; it helps a lot!
How can I modify the above function to not show the hour (9:55 in your example)?
You can remove the time as such:
add_filter( 'get_comment_time', 'remove_comment_time', 10, 1 );
function remove_comment_time( $the_time ){
return '';
}
However, you will still be left with the “at” which has no filter. You would need to find a way to remove this. For this, it would be best to reach out to a custom developer as this is WordPress core:https://wordpress.org/support/forum/wp-advanced/
Hope this clarifies. :)
I understand, I'll keep the time for that "at" :)
Alright! Feel free to reach out anytime you’ll need assistance with anything else. :)
I'm thinking that the "at" can be eliminated by translating it (Loco) with nothing "".
I just realized you can do this through PHP. There is a hook we can use to simply make a duplicate. Specifically, generate_after_comment_author_name.
Try this PHP code instead of the ones provided earlier:
add_action('generate_after_comment_text','create_new_comment_date_b');
function create_new_comment_date_b(){
$time = human_time_diff( get_comment_time('U'), current_time('timestamp') );
echo '<div class="entry-meta comment-metadata my-new-comment-meta"><a href="' . esc_url( get_comment_link( get_comment_ID() ) ) . '"><time datetime="' . get_comment_time('U') . '" itemprop="datePublished">' . sprintf( '%1$s ago',
$time
) . '</time></a></div>';
}
Then, hide the other comment meta by adding this CSS in Appearance > Customize > Additional CSS:
.comment-author-info .comment-metadata:last-child {
display: none;
}
Kindly let us know how it goes. :)
Absolutely fantastic support! Thank you very much!
You’re welcome Raul! Glad to be of assistance! :)