Using WordPress ‘get_comment_time’ PHP filter

The get_comment_time WordPress PHP filter allows you to modify the comment time displayed on your website.

Usage

add_filter('get_comment_time', 'your_custom_function', 10, 5);
function your_custom_function($date, $format, $gmt, $translate, $comment) {
    // your custom code here
    return $date;
}

Parameters

  • $date (string|int): The comment time, formatted as a date string or Unix timestamp.
  • $format (string): PHP date format.
  • $gmt (bool): Whether the GMT date is in use.
  • $translate (bool): Whether the time is translated.
  • $comment (WP_Comment): The comment object.

More information

See WordPress Developer Resources: get_comment_time

Examples

Display comment time in custom format

Change the comment time format to a custom one.

add_filter('get_comment_time', 'change_comment_time_format', 10, 5);
function change_comment_time_format($date, $format, $gmt, $translate, $comment) {
    $format = 'H:i'; // Use custom format
    return date_i18n($format, strtotime($date));
}

Display comment time in 12-hour format

Change the comment time format to a 12-hour format.

add_filter('get_comment_time', 'change_to_twelve_hour_format', 10, 5);
function change_to_twelve_hour_format($date, $format, $gmt, $translate, $comment) {
    $format = 'g:i A'; // Use 12-hour format
    return date_i18n($format, strtotime($date));
}

Display comment time as relative time

Show the comment time as a relative time, like “5 minutes ago”.

add_filter('get_comment_time', 'display_relative_time', 10, 5);
function display_relative_time($date, $format, $gmt, $translate, $comment) {
    return human_time_diff(strtotime($date), current_time('timestamp')) . ' ago';
}

Display comment time in different timezone

Display the comment time in a specific timezone instead of the default one.

add_filter('get_comment_time', 'change_comment_timezone', 10, 5);
function change_comment_timezone($date, $format, $gmt, $translate, $comment) {
    $tz = new DateTimeZone('America/New_York'); // Set the desired timezone
    $date_obj = new DateTime($date, $tz);
    return $date_obj->format($format);
}

Add a custom prefix to comment time

Add a custom prefix to the comment time, like “Posted at” before the time.

add_filter('get_comment_time', 'add_prefix_to_comment_time', 10, 5);
function add_prefix_to_comment_time($date, $format, $gmt, $translate, $comment) {
    return 'Posted at ' . date_i18n($format, strtotime($date));
}