Wordpress

WordPress Date Format: How to Change Date and Time

January 3, 2023
5 min of reading
WordPress Date Format: How to Change Date and Time
Connect with us

Join our list and receive exclusive content

In this article, we will show you how to change the date and time format in WordPress. If you want to abbreviate the month, hide the year, or display the time, you can use the built-in functions in WordPress to customize the format.

To modify your date and time format, go to the Settings » General page in the WordPress admin area and scroll down to the timezone section.

Wordpress Settings Page
Settings » General page in the WordPress admin area

You can select a timezone for your website and choose a date and time format. There are several built-in options available, or you can create a custom format by entering it manually. Simply click on the radio button next to your preferred option.

If you want to change the date and time format on your WordPress website using the template file, you can locate the WordPress tag functions the_date() and the_time(). These functions will display the date and time format that is set in your WordPress dashboard. This option is useful if your theme or plugin files have their own default date and time format.

Time and Date Formats in WordPress

On the Settings » General page, you can find a series of characters called the format string next to each option in the Date Format and Time Format sections. These format characters represent different elements of the date and time structure, and can be used to create a custom string that displays the date and time in a specific format.

For example, in the m/d/Y format string, the characters represent the following:

  • m: the numeric month with a leading zero
  • d: the numeric day of the month with a leading zero
  • Y: the four-digit year

So this string would output the date as 11/18/2022.

Since WordPress is developed in PHP, you can use the following date and time format characters to create your own custom structure string or take it directly from the PHP website.

Format CharacterDescriptionExample
Day of the Month
dNumeric, with leading zeros01-31
jNumeric, without leading zeros1-31
SEnglish suffix for the day of the monthst, nd, th in the 1st, 2nd, or 15th
Format CharacterDescriptionExample
Weekday
IFull name (lowercase ‘L’)Sunday – Saturday
jNumeric, without leading zeros1-31
DThree letter nameMon – Sun
Format CharacterDescriptionExample
Month
mNumeric, with leading zeros01-12
nNumeric, without leading zeros1-12
FTextual fullJanuary – December
MTextual three lettersJan – Dec
Format CharacterDescriptionExample
Year
YNumeric, 4 digitsEg., 2018, 2023
yNumeric, 2 digitsEg., 18, 23
Format CharacterDescriptionExample
Time
aLowercaseam, pm
AUppercaseAM, PM
gHour, 12-hour, without leading zeros1-12
hHour, 12-hour, with leading zeros01-12
GHour, 24-hour, without leading zeros0-23
HHour, 24-hour, with leading zeros00-23
iMinutes, with leading zeros00-59
sSeconds, with leading zeros00-59
TTimezone abbreviationEg., EST, MDT …
Format CharacterDescriptionExample
Full Date/Time
cISO 86012004-02-12T15:19:21+00:00
rRFC 2822Thu, 21 Dec 2000 16:01:07 +0200
UUnix timestamp (seconds since Unix Epoch)1455880176

Here are some examples of date format with the resulting output.

Format StringExample
F j, Y g:i aNovember 6, 2010 12:50 am
F j, YNovember 6, 2010
F, YNovember, 2010
g:i a12:50 am
g:i:s a12:50:48 am
l, F jS, YSaturday, November 6th, 2010
M j, Y @ G:iNov 6, 2010 @ 0:50
Y/m/d at g:i A2010/11/06 at 12:50 AM
Y/m/d at g:ia2010/11/06 at 12:50am
Y/m/d g:i:s A2010/11/06 12:50:48 AM
Y/m/d2010/11/06

Additional Customization Options

Localizing

To localize the date and time on your WordPress site, you can use the wp_date() function. This function works similarly to the PHP date() function, but it also translates things like month names and weekdays into the current locale for the site. You can use wp_date() instead of date(), using the same arguments.

For example, instead of using date('F jS, Y'), you can use wp_date('F jS, Y'). This will translate the month and day names into the current locale for the site.

Escaping

It’s important to note that some letters do not have an associated format in the PHP date() function. For example, if you pass the letter ‘x’ in the format string, it will currently return a literal ‘x’. However, this could change in the future and ‘x’ may have a format associated with it.

To prevent this from causing issues, it’s recommended to always escape literal characters in a date-formatted string using a backlash “\”. For example, in the following example, every letter in the word ‘of’ is escaped:

wp_date( __( 'l jS \o\f F Y', 'textdomain' ) );

This example will render as follows on the front end of your site: Monday 24th of October 2022.

Conclusion

In conclusion, the WordPress date() function allows you to format and display the date and time on your website according to your preferred structure. You can use a variety of predefined format characters, or create a custom format string using a combination of these characters.

To localize the date and time to the current locale for your site, you can use the wp_date() function instead of date(). It’s important to remember to escape literal characters in your format string using a backslash “\”, as this will ensure that your format is displayed correctly even if the meaning of certain characters changes in the future.

By using these functions, you can easily customize the way the date and time are displayed on your WordPress website.