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|
|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|
|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|
|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|
|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().
||November 6, 2010 12:50 am|
||November 6, 2010|
||Saturday, November 6th, 2010|
||Nov 6, 2010 @ 0:50|
||2010/11/06 at 12:50 AM|
||2010/11/06 at 12:50am|
||2010/11/06 12:50:48 AM|
Example with WordPress function
Post was published at <?php the_time('j F Y H:i') ?> and was placed in categories <?php the_category(', ') ?>.
Post was published at 25 March 2018 12:33 and was placed in categories WordPress, Codex.
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
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') );