Using WordPress ‘get_the_date’ PHP filter

The get_the_date WordPress PHP filter allows you to modify the date format of a post’s published date.

Usage

add_filter('get_the_date', 'your_custom_function', 10, 3);

function your_custom_function($the_date, $format, $post) {
    // your custom code here
    return $the_date;
}

Parameters

  • $the_date (string|int) – Formatted date string or Unix timestamp if $format is ‘U’ or ‘G’.
  • $format (string) – PHP date format.
  • $post (WP_Post) – The post object.

More information

See WordPress Developer Resources: get_the_date

Examples

Change date format

Change the date format to “F j, Y”.

add_filter('get_the_date', 'change_date_format', 10, 3);

function change_date_format($the_date, $format, $post) {
    return get_post_time('F j, Y', false, $post, true);
}

Add custom text before date

Add “Published on: ” before the date.

add_filter('get_the_date', 'add_custom_text_before_date', 10, 3);

function add_custom_text_before_date($the_date, $format, $post) {
    return 'Published on: ' . $the_date;
}

Display time since published

Display time since the post was published (e.g., “2 hours ago”).

add_filter('get_the_date', 'display_time_since_published', 10, 3);

function display_time_since_published($the_date, $format, $post) {
    return human_time_diff(get_post_time('U', true, $post)) . ' ago';
}

Add custom CSS class to date

Wrap the date with a span element and add a custom CSS class.

add_filter('get_the_date', 'add_custom_css_class_to_date', 10, 3);

function add_custom_css_class_to_date($the_date, $format, $post) {
    return '<span class="custom-date-class">' . $the_date . '</span>';
}

Display date in a custom language

Display the date in French.

add_filter('get_the_date', 'display_date_in_french', 10, 3);
function display_date_in_french($the_date, $format, $post) {
setlocale(LC_TIME, 'fr_FR');
return strftime('%d %B %Y', strtotime($the_date));
}