get_comment_date()
Gets the date of the specified comment.
Uses: mysql2date()
Used By: comment_date()
Hooks from the function
Returns
String. Formatted date of the comment.
Usage
get_comment_date( $d, $comment_ID );
- $d(string)
Date format. By default, the format option
get_option('date_format')will be used. This format is set in the site settings. List of formatsDefault: ''
- $comment_ID(int/object)
- ID or object of the comment. If left empty, the current comment will be used.
Default: 0
Examples
#1 Get the date of the comment in the format we need:
$format = 'l, F jS, Y'; $comment_ID = 25; $comment_date = get_comment_date( $format, $comment_ID ); echo $comment_date; // "Wednesday, November 6, 2013.
#2 Display a beautiful, human-readable, comment time:
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' )
)
);
}
When called it will convert the time and return something like:
"1 min ago", "3 mins ago", "17 hours ago", "7 days ago", "2 weeks ago", etc...
#3 Examples of Different 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' );
See also: Date and Time Formats in WordPress.
Changelog
| Since 1.5.0 | Introduced. |
| Since 4.4.0 | Added the ability for $comment_id to also accept a WP_Comment object. |
get_comment_date() get comment date code WP 6.9.1
function get_comment_date( $format = '', $comment_id = 0 ) {
$comment = get_comment( $comment_id );
$_format = ! empty( $format ) ? $format : get_option( 'date_format' );
$comment_date = mysql2date( $_format, $comment->comment_date );
/**
* Filters the returned comment date.
*
* @since 1.5.0
*
* @param string|int $comment_date Formatted date string or Unix timestamp.
* @param string $format PHP date format.
* @param WP_Comment $comment The comment object.
*/
return apply_filters( 'get_comment_date', $comment_date, $format, $comment );
}