wordpress date php

WordPress Date and Time PHP Development

WordPress retrieves and displays the date and time with format using template tags, such as the_date() and the_time() functions in the archive page or single post templates. Some template tags can be used with format parameters to specify how the output of the date is displayed. In this article, we are going to discuss how to display the date and time in WordPress and how to add and develop them in your custom theme inside the loop even with the different timezone.

The Available WordPress date and time functions

WordPress gives you five functions to output the date and/or time:

  • the_date(): By default, it will echo the date of the post in the format F j, Y.
  • get_the_date(): This fetches the date and doesn’t echo it out. To echo it, you’d use echo get_the_date(), which gives you the same result as the_date().
  • the_time() and get_the_time(): These fetch the time by default, but if you specify date formatting, you can also include the date.
  • wp_date(): Retrieves the date with the custom local timezone.

How to display the published date of the post?

Every post in WordPress has its own published date and time. To display the published date of the post you can use the the_date() or get_the_date() functions.

the_date();
echo get_the_date();

Output

November 6, 2022

The difference in the functions is in the echo. the_date() function doesn’t need the echo and doesn’t need the sanitization.

But the get_the_date() function needs both (echo and sanitization).

echo esc_html(get_the_date());

Read more about Data Sanitization and Validation in WordPress

How to display the published time of the post?

Every post in WordPress has its own published date and time. To display the published time of the post you can use the the_time() or get_the_time() functions.

the_time();
echo get_the_time();

Output

6:33 am

The difference in the functions is in the echo. the_time() function doesn’t need the echo and doesn’t need the sanitization.

But the get_the_time() function needs both (echo and sanitization).

How to display WordPress post date and time in format

Each function has a default format, which you can override if you need to. You can change the format of the date or time in WordPress by passing the parameter of the format to the functions.

The date and time format are based on alphabetic characters that represent a time format in WordPress. For example, entering Y will output the year in numeric four digits, like 2022. Entering smaller case y will output the year in numeric two digits, like 22.

the_date("j M, Y");
echo get_the_date("j M, Y");

Output

6 Nov, 2022

Format parameters list:

  • d – The day of the month (from 01 to 31)
  • D – A textual representation of a day (three letters)
  • j – The day of the month without leading zeros (1 to 31)
  • l (lowercase ‘L’) – A full textual representation of a day
  • N – The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
  • S – The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
  • w – A numeric representation of the day (0 for Sunday, 6 for Saturday)
  • z – The day of the year (from 0 through 365)
  • W – The ISO-8601 week number of the year (weeks starting on Monday)
  • F – A full textual representation of a month (January through December)
  • m – A numeric representation of a month (from 01 to 12)
  • M – A short textual representation of a month (three letters)
  • n – A numeric representation of a month, without leading zeros (1 to 12)
  • t – The number of days in the given month
  • L – Whether it’s a leap year (1 if it is a leap year, 0 otherwise)
  • o – The ISO-8601 year number
  • Y – A four-digit representation of a year
  • y – A two-digit representation of a year
  • a – Lowercase am or pm
  • A – Uppercase AM or PM
  • B – Swatch Internet time (000 to 999)
  • g – 12-hour format of an hour (1 to 12)
  • G – 24-hour format of an hour (0 to 23)
  • h – 12-hour format of an hour (01 to 12)
  • H – 24-hour format of an hour (00 to 23)
  • i – Minutes with leading zeros (00 to 59)
  • s – Seconds, with leading zeros (00 to 59)
  • u – Microseconds (added in PHP 5.2.2)
  • e – The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
  • I (capital i) – Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
  • O – Difference to Greenwich time (GMT) in hours (Example: +0100)
  • P – Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
  • T – Timezone abbreviations (Examples: EST, MDT)
  • Z – Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
  • c – The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
  • r – The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
  • U – The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

As you know, WordPress is written in PHP, then you could use PHP date format in WordPress too.

You can format the time too.

the_time("h:i A");

Output

06:33 AM

If you don’t know which one to use (the_time or the_date) you can use one of them with formatting.

the_time("j M, Y h:i:s a");
		
the_date("j M, Y h:i:s a");

Both outputs are:

6 Nov, 2022 06:33:45 am

6 Nov, 2022 06:33:45 am

If you use the format parameter, you could use one of the functions in your code and there is no difference in the output.

Note that format parameters are case-sensitive, and commas (,) and slashes (/) are displayed exactly as written in the format string between the parentheses.

You can modify your format from Dashboard by going to Settings » General page in the WordPress admin area and scrolling down to the timezone section.

Display the date and time in the loop of the WordPress

Let’s say you are going to display the publish date and time of the post in the archive or single post page. The above functions could be used in the loop.

while ( have_posts() ) :
	the_post();
        .
        .
        the_date("j M, Y h:i:s a");
        .
        .
endwhile;

Display the date and time of the post with the post ID

If you are going to show the date and time of the post outside of the loop, you could use the second parameter as an ID to get the date.

echo get_the_date("j M, Y h:i:s a", $post_id);

In this example, you should use the get_the_date() function because the the_date() function doesn’t support the post ID.

Add before and after content to the WordPress date

In the previous example, you can’t display the date with the post ID by the_date() function. But with this function, you can add before and after content or even HTML codes to your post date.

the_date("j M, Y h:i:s a", '<span class="published-date">','</span>');

HTML output

<span class="published-date">6 Nov, 2022 06:33:45 am</span>

In the above examples, we discussed the get_the_date() and the_time() functions and you can use this function like the the_date(). In this example, the the_time() doesn’t support the before and after content.

Display the date and time in a custom local timezone in WordPress

If you want to display the published date of the WordPress post in the custom local timezone you have to use the wp_date() function.

$timezone = new DateTimeZone( 'Asia/Seoul' );
echo wp_date("j M, Y h:i:s a", get_post_timestamp(), $timezone);

Output

6 Nov, 2022 03:33:45 pm

This example shows the date in the Seoul timezone. As you could see, the 6 am was changed to 3 pm.

The first parameter is the format, the second is the time stamp of the post and the third parameter is the timezone.

Note that this function only can be used in the WordPress loop and is not available outside the loop.

Display the last modify or update date and time

Some websites update their posts regularly. Showing when each post was updated makes sure your readers don’t miss any content, and helps build trust. If you want to display the last update and modified date of the post, you can use the following functions.

the_modified_date("j M, Y h:i:s a");

Output

17 Nov, 2022 08:49:24 am

These WordPress functions like the above functions accept date format and even custom timezone.

Get the creation date of the post in WordPress

There is no built-in function to get the creation date of the post directly in WordPress. To get the date you have to use post meta to get the values of the _wp_old_date. The first value is the creation date.

$dates = get_post_meta( get_the_ID(), '_wp_old_date' );
if(count($dates) > 0){
	echo $dates[0];
}

Output

2022-11-07 

If you never changed the post’s date, _wp_old_date will be empty. To deal with it, we need to check if _wp_old_date is empty and if it is, we fall back to our familiar get_the_date(). Because the creation and published date are the same.

The get_post_meta() accepts a third, optional parameter which if set to true will return only the first value. So, we can easily get the creation date of the post simply by using the below code.

get_post_meta( get_the_ID(), '_wp_old_date', true );.

In this example, if you want to format or display at different timezone you have to use formatting functions of the PHP and WordPress.

How to get the date and time format of the WordPress settings

To get the format of the date and time of the WordPress settings simply you can use the get_option() function like the following code.

get_option('date_format');
get_option('time_format');

Missing Dates in an Archive Page

Sometimes on the archive page, the the_date() function doesn’t show the date. This is because of the date of the post. If your two or more posts are published on the same day, the_date() doesn’t display the date of the second post.

To solve this problem you can use the get_the_date() function and the rest functions. The function get_the_date() returns the date for all posts, even if they are published on the same date.

You may like

Shopping Cart