Using WordPress ‘get_the_date’ PHP function

The get_the_date() WordPress PHP function retrieves the date on which a post was written. This function always returns the date and its output can be modified using the ‘get_the_date’ filter.

Usage

echo get_the_date( $format, $post );
// your custom code here

Parameters

  • $format (string, optional): PHP date format. Defaults to the ‘date_format’ option. Default: ”.
  • $post (int|WP_Post, optional): Post ID or WP_Post object. Default current post. Default: null.

More information

See WordPress Developer Resources: get_the_date()

Examples

Display the post date with the default format

echo get_the_date();

Display the post date with a custom format

echo get_the_date('F j, Y');

Display the post date for a specific post ID

$post_id = 42;
echo get_the_date('', $post_id);

Display the post date with a custom format and a specific post ID

$post_id = 42;
echo get_the_date('F j, Y', $post_id);

Modify the output using the ‘get_the_date’ filter

add_filter('get_the_date', 'customize_post_date', 10, 3);
function customize_post_date($date, $format, $post_id) {
    // your custom code here
    return $date;
}