How to Get Post Date with Custom Format in WordPress! (+Shortcode)

5 from 5
from 1 user
(Rate this post)

Do you need to get the publish date for the active post? or just get the publish date by specifying a post ID? in this WordPress tutorial, we used a particular WP function to retrieve the publish date of any post with various formats. We used PHP codes to get the post date of a requested post, but it can be wrapped into a shortcode.

There is a function in WordPress that can retrieve a post’s publish date.

get_the_date() is the function responsible for getting the publish date of a post.

get_the_date() accepts an optional ‘format’ parameter, which is helpful when you want to customize the output string.

If you do not specify the date format, get_the_date() will get it from WordPress settings (Path: Admin Panel -> Settings -> General).

WordPress Panel -> Settings -> General

Get the Active Post Publish Date in WordPress

To retrieve the Publish Date of the current queried post in WordPress, use this as an example:

$postDate = get_the_date();

//printing the active post date with WP setting's format:
echo $postDate;

To specify the date format, use this example for Active Post:

//Storing a few possible date formats in an array:
$postDateFormats = [
            'F j, Y', // ex: September 1, 2022
            'jS M Y', // ex: 1st Sep 2022
            'Y/m/d g:i:s A', // ex: 2022/09/01 6:09:06 AM
            'Y/m/d \a\t g:i A', // ex: 2022/09/01 at 6:09 AM
            'Y/m/d g:i:s A', // ex: 2022/09/01 6:09:06 AM
            'c', // ex: 2022-09-01T06:09:06-04:00(ISO format for schema)
            'r', // ex: Thu, 01 Sep 2022 06:09:06 -0400 (RFC 2822)
            'u', // ex:  1662027259 (Unix timestamp)
];

//print post date using them in different lines:
foreach($postDateFormats as $format){
	echo get_the_date($format)." 
"; }

The above example used a loop to go through each post format and print the date.

You can select one of them and use it; for example, to use September 1, 2022 output format, use: get_the_date('F j, Y').

Get the Post Publish Date for Specified Post ID in WordPress

To specify the post ID, Use the second parameter of the get_the_date function.

For example, to get the publish date for a post with ID = 1, Use this code:

$postId = 1;

$postDate = get_the_date('',$postId);

//printing the post date with WP setting's format:
echo $postDate;

To specify the date format and the Post ID at the same time, use this example (we used post ID = 1):

//define post ID you want the date for:
$postId = 1;

//Storing a few possible date formats in an array:
$postDateFormats = [
            'F j, Y', // ex: September 1, 2022
            'jS M Y', // ex: 1st Sep 2022
            'Y/m/d g:i:s A', // ex: 2022/09/01 6:09:06 AM
            'Y/m/d \a\t g:i A', // ex: 2022/09/01 at 6:09 AM
            'Y/m/d g:i:s A', // ex: 2022/09/01 6:09:06 AM
            'c', // ex: 2022-09-01T06:09:06-04:00(ISO format for schema)
            'r', // ex: Thu, 01 Sep 2022 06:09:06 -0400 (RFC 2822)
            'u', // ex:  1662027259 (Unix timestamp)
];

//print post date using them in different lines:
foreach($postDateFormats as $format){
	echo get_the_date($format, $postId)." 
"; }

The above example used a loop to go through each post format and print the date for the specified post.

You can select one of them and use it; for September 1, 2022 output format, use: get_the_date('F j, Y', $postId).

Printing the Post Publish Date Using a Shortcode

To print the publish date with a shortcode, first, we have to define the shortcode and then use it in the WordPress editor.

Step 1: Register the [my-post-date] Shortcode in WordPress

open the functions.php file of your active theme or your custom-made plugin main PHP file, and copy/paste this snippet into it:

if(!function_exists('my_post_date_callback')){

	//register [my-post-date] shortcode
	add_shortcode( 'my-post-date', 'my_post_date_callback' );

	/*
	define [my-post-date] callback
	-> use date-format to define date format
	-> use post-id to specify a post id
	ex: [my-post-date date-format='F j, Y' post-id=1]
	*/
	function my_post_date_callback($attributes) {

		$dateFormat = '';

		$postId = null;

		//check if date-format attribute is present in shortcode
		if(isset($attributes["date-format"]))
			$dateFormat = $attributes["date-format"];

		//check if post-id attribute is present in shortcode
		//and make sure value is numeric
		if(isset($attributes["post-id"]) && is_numeric($attributes["post-id"]))
			$postId = $attributes["post-id"];

		//do not use echo, just use return
		return get_the_date($dateFormat, $postId);
	}
	
}

Step 2: Use the [my-post-date] Shortcode in WordPress

Open a post or create a new one. in the editor, just write [my-post-date] in the content part. This will print the active post date with the default format on that post page.

If you want to print the date in a special format, use the post-format attribute for the shortcode. (Ex: [my-post-date date-format='F j, Y'])

If you want to print the date for a specific post ID, use the post-id attribute for the shortcode. (Ex: [my-post-date date-format='F j, Y' post-id=1])

What do you think about "How to Get Post Date with Custom Format in WordPress! (+Shortcode)"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu