WordPress at a glance

Date and Time Formats in WordPress

WordPress has functions that get the date or time of a post, comment, or something else: the_date(), the_time(), mysql2date() etc. You can pass a format to each of these functions to get the time or date in the desired format. This post shows how to get desired format and which character in the specified format are responsible for what.

A simple example of displaying the time when the post was published:

<?php the_time('j F Y H:i'); ?>

Displays the date as: 25 March 2018 12:33.

All date and time format characters

Now, let's look at all the special characters that can be used in the format.

Day of month
d 01–31 Number, with zero ahead
j 1–31 Number, no leading zero
S st, nd or th in the 1st, 2nd or 15th. English suffix for day of month
Weekday
l Sunday – Saturday Full name (lowercase 'L')
D Mon – Sun (Пон - Вс) Short name
N 1 (monday) - 7 (sunday) The day of the week
w 0 (sunday) - 6 (saturday) The day of the week
Month
m 01–12 Number, with zero ahead
n 1–12 Number, no leading zero
F January – December Full name
M Jan - Dec Short name
t 28 - 31 Number of days in month
Year
Y 1999, 2003 Number, 4 digits
y 99, 03 Number, 2 digits
z 0 - 365 Day number in year (starting from 0)
W 34 (34th week of the year) Year week number
L 1 or 0 Whether it is a leap year
Time
a am, pm String range
A AM, PM Capital range
h 01–12 Hours (0-12), with zero ahead
g 1–12 Hours (0-12), no leading zero
H 00-23 Hours (0-24), with zero ahead
G 0-23 Hours (0-24), no leading zero
i 00-59 Minutes, with zero ahead
s 00-59 Seconds, with zero ahead
T EST, MDT ... Abbreviation of the time zone
Full date and time
c 2004-02-12T15:19:21+00:00 Date in ISO 8601 format
r Thu, 21 Dec 2000 16:01:07 +0200 Date in RFC 2822 format
U 1455880176 Number, Unix time stamp-number of seconds since January 1, 1970
e UTC, GMT, Atlantic/Azores Timezone identifier

The formats from the table are the PHP standard and can also be used to format dates in PHP, for example using a function date().

Examples

Format Result
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
Y-m-d h:i:s 2017-11-06 21:37:22

Example with WordPress function

Post was published at <?php the_time('j F Y H:i') ?> and was placed in categories <?php the_category(', ') ?>.

Displays:

Post was published at 25 March 2018 12:33 and was placed in categories WordPress, Codex.

Escaping

If you need use characters from the table above as it is, without converting it to anything, you must prepend each symbol with backslash \:

echo get_the_time('j F Y \t\i\m\e H:i'); //> 25 March 2018 time 12:33

Localization

To translate strings in a date like "November" to the language of the site, use function date_i18n().

You can also use the standard localization functions to localize the format: __(), _e() etc. Example:

echo get_the_date( __('j F Y \t\i\m\e H:i','dom') );
No comments
    Hello, !     Log In . Register