get_comment_date()
Retrieve the comment date of the current comment.
Uses: mysql2date()
Used By: comment_date()
Hooks from the function
Return
String
. The comment's date.
Usage
get_comment_date( $format, $comment_id );
- $format(string)
- PHP date format. option.
Default: 'date_format' - $comment_id(int|WP_Comment)
- WP_Comment or ID of the comment for which to get the date.
Default: current comment
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.7.2
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 ); }