The comment_date() WordPress PHP function is used to display the date of the current comment. The format of the date can be customized, and it defaults to the ‘date_format’ option in your WordPress settings. You can use this function to print the date for a specific comment or the currently active comment in a comments loop.
Usage
You can use the function comment_date() like so:
comment_date('F j, Y');
This will print the date of the current comment in the format ‘Month Day, Year’. For example, ‘January 1, 2023’.
Parameters
- $format (string): This is an optional parameter where you can specify a custom PHP date format. If left blank, it defaults to the ‘date_format’ option in your WordPress settings.
- $comment_id (int|WP_Comment): This is another optional parameter. You can provide either the ID of a specific comment or an instance of the WP_Comment class to print the date for that comment. If left blank, it defaults to the currently active comment in a comments loop.
More information
See WordPress Developer Resources: comment_date()
This function is part of the WordPress core and is not deprecated. It is located in the wp-includes/comment-template.php
file.
Examples
Basic Usage
Display the date of the current comment in the default format.
comment_date();
Custom Date Format
Display the date of the current comment in the ‘F j, Y’ format.
comment_date('F j, Y');
Specific Comment ID
Display the date of the comment with the ID of 10 in the default format.
comment_date('', 10);
Specific Comment ID and Custom Format
Display the date of the comment with the ID of 20 in the ‘F j, Y’ format.
comment_date('F j, Y', 20);
Using WP_Comment Object
First, get a WP_Comment object for a specific comment. Then, display the date of that comment in the default format.
$comment = get_comment(30); comment_date('', $comment);