
In this tutorial, we will learn about how we can display the date and time in WordPress using the core functions of WordPress and PHP date functions.
WordPress does not come with a built-in function to show the current date-time but we can still show it using the PHP date-time functions.
But if you want to display the date and time in WordPress posts then you can use the built-in WordPress functions. You can also change the format of date-time in the posts loop.
Let’s get started to jump on the main point of showing the date and time or changing the format in WordPress posts or theme files.
See Also: PHP – Date and Time Functions
Display Date and Time in WordPress Posts
To print the date and time in WordPress posts, you can use the following four methods. It will show you the current date and time.
Date and Time Functions in WordPress.
Format String Examples: we will use some of these in this tutorial.
- F j, Y g:i a – November 6, 2010 12:50 am
- F j, Y – November 6, 2010
- F, Y – November, 2010
- g:i a – 12:50 am
- g:i:s a – 12:50:48 am
- l, F jS, Y – Saturday, November 6th, 2010
- M j, Y @ G:i – Nov 6, 2010 @ 0:50
- Y/m/d \a\t g:i A – 2010/11/06 at 12:50 AM
- Y/m/d \a\t g:ia – 2010/11/06 at 12:50am
- Y/m/d g:i:s A – 2010/11/06 12:50:48 AM
- Y/m/d – 2010/11/06
We will discuss these methods and formats strings in detail and see the examples. WordPress also allows us to modify these functions to change the format to display the date and time.
WordPress the_date() Function
The the_date()
function is used to retrieve the date of the current post when it was published. It will only output the date if the current post’s date is different from the previous one output.
Syntax
$format:
to set the format of the date, the default format is F j, Y
$before:
it is the prefix of date output, you can add starting HTML tag to wrap it (<p>)
$after:
it is the suffix of date output, you can add the closing HTML tag (</p>)
$echo:
set true value for echoing the date and false for returning the date
Examples of the_date() functions:
To see the more date format strings, you can check here.
WordPress get_the_date() Function
The get_the_date()
function is also used to retrieve the post date when it was written or published.
We used this function when we are already using echo in our code to print all the stuff. Basically, this function only fetches the date of the post but does not print it out.
To print the date with the get_the_date() function, you have to use the echo with it. Then it will give same result as the_date()
function.
Syntax
$format:
to set the date format, default is the value of get_option(‘date_format’)
$post:
to get the date of a specific post by passing the post ID or post object.
Examples of get_the_date() function:
To see the more date format strings, you can check here.
WordPress the_time() Function
The the_time()
function is used to display the time of the post when that was published. We mostly use this function in the loop of posts to show the post time in different formats like 5 minutes ago, 2 days ago, etc.
Syntax
$format:
to set the time format, default is the value of get_option(‘time_format’). Accepts ‘G’, ‘U’ or PHP date time format
Examples of the_time() function:
WordPress get_the_time() Function
The get_the_time()
function works as same the the_time() function, it fetches the time of the post but you have to use the echo to display the time. You can also print the time of a specific post by passing the post ID.
Syntax
$format
: to set the time format, default is the value of get_option(‘time_format’). Accepts ‘G’, ‘U’ or PHP date time format
$post
: object of posts or post ID
Examples of get_the_time() function:
Display Date and Time in WordPress Template Files
As you know WordPress does not have any built-in date-time functions to print the current date and time. So, to print the today’s date in wordpress theme’s template file you can use the PHP date time functions.
Use the following code in WordPress to print the current date and time.
To display date in default WordPress date format with PHP date function, use the following code where you want to print it.
You can also print the date in different formats using the pre-defined format string. See the following example code.
To play with PHP date functions in wordpress, check my PHP date & time functions tutorial for more in details.
More Useful Date Time Functions in WordPress
Here we have some more useful functions that you can also use them to display the date and time in wordpress posts or in different formats.
Display Post Modified Date
To print the modified date of post when it was edited, then you can use the get_the_modified_date()
function.
Display Time in Unix Timestamp
To print the local time of the post in Unix timestamp format, you have to pass the ‘U’ format string. It will print the local timestamp.
In most cases, you would probably want the epoch time for GMT (rather than for the local time zone), which you can get with the get_post_time()
function.
I hope this tutorial is helpful for you to learn how to display the date and time in wordpress posts and theme’s template files. If any doubt please ask in the comment section.
See Also: How to Add and Subtract Days, Month and Year From Date in PHP?