The comment_time() WordPress PHP function is used to display the time of the current comment. It accepts a custom PHP time format and can be applied to a specific comment by using its ID or WP_Comment object.
Usage
To use the function, you can simply call it without any parameters to display the time of the current comment in the default format:
comment_time();
To customize the format or specify a comment, you can provide arguments:
comment_time('H:i:s', $comment_id);
Parameters
- $format (string, optional) – PHP time format. If not provided, it defaults to the ‘time_format’ option.
- $comment_id (int|WP_Comment, optional) – The ID or WP_Comment object of the comment for which to print the time. If not provided, it defaults to the current comment.
More information
See WordPress Developer Resources: comment_time()
Examples
Displaying Comment Time in Default Format
This displays the time of the current comment in the default format:
comment_time(); // Output: 22:04:11 (based on the 'time_format' option)
Displaying Comment Time in Custom Format
This displays the time of the current comment in a custom format:
comment_time('H:i:s'); // Output: 22:04:11
Displaying Time of a Specific Comment in Default Format
This displays the time of a specific comment in the default format:
$comment_id = 123; // Replace with your comment ID comment_time('', $comment_id); // Output: 22:04:11 (based on the 'time_format' option)
Displaying Time of a Specific Comment in Custom Format
This displays the time of a specific comment in a custom format:
$comment_id = 123; // Replace with your comment ID comment_time('H:i:s', $comment_id); // Output: 22:04:11
Using WP_Comment Object to Display Time in Custom Format
This uses a WP_Comment object to display the comment’s time in a custom format:
$comment = get_comment(123); // Replace with your comment ID comment_time('H:i:s', $comment); // Output: 22:04:11