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
Microseconds
u 012345 Microseconds - 'H:i:s.u' > 08:07:41.285872
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.

Options related to dates

var_dump( [
	'date_format'     => get_option( 'date_format' ),
	'time_format'     => get_option( 'time_format' ),
	'gmt_offset'      => get_option( 'gmt_offset' ), // may be string - depends on value of `timezone_string`
	'offset'          => (float) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS,
	'start_of_week'   => (int) get_option( 'start_of_week', 0 ),
	'timezone_string' => get_option( 'timezone_string' ),
] );

/*
array(6) {
  ["date_format"]=> string(5) "j M Y"
  ["time_format"]=> string(3) "H:i"
  ["gmt_offset"]=> float(5)
  ["offset"]=> float(18000)
  ["start_of_week"]=> int(1)
  ["timezone_string"]=> string(0) "Asia/Tashkent"
}
*/

Escaping

If you need to 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 - a format is \j \F \Y'); //> 25 March 2018 a format is j F Y

Localization

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

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') );