Get the date of a WordPress comment with get_comment_date

The WordPress get_comment_date function is responsible for retrieving the date of a specific comment. It is a part of WordPress’s built-in functionality for handling comments and their associated data.

This function can be used to display the date when a comment was posted on a website. It is particularly relevant in scenarios where the date of the comment is necessary, such as on blog posts, forums, or any other platform where user comments are displayed. This allows visitors to the site to understand the timeline of the discussion or conversation.

When this function is called, it returns the date of the comment in a format specified by the site’s settings or a format passed to the function. This can be useful for maintaining consistency across the site, as the date format will match the rest of the site’s content.

The get_comment_date function operates by retrieving the date information from the WordPress database for a specific comment, based on the comment’s unique ID. It then processes this date information and returns it in the desired format.

Parameters of the get_comment_date Function in WordPress

The get_comment_date function in WordPress can accept two parameters:

  • $format (string) – This parameter is optional and its default value is an empty string. It is used to specify the PHP date format. If not provided, the function will use the value of the ‘date_format’ option.
  • $comment_id (intWP_Comment) – This parameter is also optional and it is used to specify the WP_Comment or the ID of the comment for which the date should be returned. If not provided, the function will use the current comment.

Return Value of the get_comment_date Function

The get_comment_date function returns a string value which represents the date of the comment. If the function does not receive any parameters, it will return the date of the current comment.

Examples

How to Display the Date of a Comment

$comment_id = 123; // replace with your comment ID
$date_format = 'F j, Y'; // replace with your desired date format
echo '<p>' . get_comment_date($date_format, $comment_id) . '</p>';

This snippet displays the date of the comment with the ID stored in $comment_id in the format specified by $date_format. The date is wrapped in a paragraph HTML tag.

How to Check if a Comment was Made Today

$comment_id = 123; // replace with your comment ID
if (get_comment_date('Ymd', $comment_id) == date('Ymd')) {
 echo '<p>This comment was made today.</p>';
} else {
 echo '<p>This comment was not made today.</p>';
}

This snippet checks if the comment with the ID stored in $comment_id was made today. If the comment was made today, it displays “This comment was made today.” If not, it displays “This comment was not made today.” The messages are wrapped in paragraph HTML tags.

How to Display the Date of the Most Recent Comment

$comments = get_comments();
if (!empty($comments)) {
 $latest_comment = $comments[0];
 echo '<p>The most recent comment was made on ' . get_comment_date('F j, Y', $latest_comment->comment_ID) . '.</p>';
} else {
 echo '<p>There are no comments.</p>';
}

This snippet retrieves all comments and checks if there are any. If there are, it gets the date of the most recent comment (the first comment in the array) and displays it in the format ‘F j, Y’. If there are no comments, it displays “There are no comments.” The messages are wrapped in paragraph HTML tags.

Conclusion

The get_comment_date function in WordPress is a tool that retrieves the date of a posted comment. This function plays a crucial role in the organization and management of comments on a WordPress site, allowing developers to access and display the date when a comment was posted. This can be particularly useful in various situations, such as sorting comments by date, displaying the date of the most recent comment, or simply providing users with more information about the timing of a discussion.

Related WordPress Functions