Using WordPress ‘get_comment_date()’ PHP function

The get_comment_date() WordPress PHP function retrieves the comment date of the current comment.

Usage

get_comment_date( $format, $comment_id )

Example:

Input: get_comment_date('F j, Y', 15)

Output: "September 8, 2021"

Parameters

  • $format (string) Optional: PHP date format. Defaults to the ‘date_format’ option. Default: ''
  • $comment_id (int|WP_Comment) Optional: WP_Comment or ID of the comment for which to get the date. Default current comment.

More information

See WordPress Developer Resources: get_comment_date()

Examples

Display human-readable comment time

This example shows how to create a function that returns a human-readable comment time, such as “3 mins ago” or “2 weeks ago”.

function smk_get_comment_time( $comment_id = 0 ) {
    return sprintf( _x( '%s ago', 'Human-readable time', 'text-domain' ), human_time_diff( get_comment_date( 'U', $comment_id ), current_time( 'timestamp' ) ) );
}

Basic example

This example shows how to use get_comment_date() with a specific date format and a comment ID.

$d = "l, F jS, Y";
$comment_date = get_comment_date( $d, $comment_ID );
echo $comment_date; // Outputs: "Saturday, November 6th, 2010"

Example with different date formats

These examples demonstrate how to use get_comment_date() with various date formats.

// Prints something like: Monday 8th of August 2005
echo get_comment_date( 'l jS \\of F Y' );

// Prints something like: Mon Mar 8 2012
echo get_comment_date( 'D M j Y' );

// Prints something like 07/08/2017 (dd/mm/yyyy)
echo get_comment_date( 'd\\/m\\/Y' );