Using WordPress ‘get_the_date()’ PHP function

The get_the_date() WordPress PHP function retrieves the date on which the post was written.

Usage

get_the_date( $format, $post )

Example: get_the_date('F j, Y') will return ‘May 6, 2023’ for a post published on May 6, 2023.

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

Displaying the date in a custom format

To display the date in the format ‘Monday January 11, 2017’, use the following code:

$post_date = get_the_date( 'l F j, Y' );
echo $post_date;

Displaying the date with day, day symbol, short month, and long year

To display the date as ’15th Jan 2020′, use the following code:

echo get_the_date( 'dS M Y', $post->ID );

Displaying the default date format

To display the default date format, use the following code:

echo '<span class="entry-date">' . get_the_date() . '</span>';

Displaying the date in Ymd format

To display the date in Ymd format (e.g., 20191231), use the following code:

$post_date = get_the_date( 'Ymd' );
echo $post_date;

Displaying the current month’s full name and year

To display the current month’s full name and year (e.g., ‘January 2020’), use the following code:

echo get_the_date( 'F Y', get_the_ID() );