date_i18n()
Retrieve the date in localized format, based on timestamp.
If the locale specifies the locale month and weekday, then the locale will take over the format for the date. If it isn't, then the date format string will be used instead.
Note that due to the way WP typically generates a sum of timestamp and offset with strtotime(), it implies offset added at a _current_ time, not at the time the timestamp represents. Storing such timestamps or calculating them differently will lead to invalid output.
Hooks from the function
Return
String
. The date, translated if locale specifies it.
Usage
date_i18n( $format, $timestamp_with_offset, $gmt );
- $format(string) (required)
- Format to display the date.
- $timestamp_with_offset(int|true|false)
- A sum of Unix timestamp and timezone offset in seconds.
Default: false - $gmt(true|false)
- Whether to use GMT timezone. Only applies if timestamp is not provided.
Default: false
Examples
#1 Localization of the php function date() in WordPress
Demonstration of the function:
// current time echo date_i18n( 'j F Y H:i:s', false ); // 12 Aug 2018 02:08:59 echo date_i18n( 'j F Y H:i:s', false, true ); // August 11, 2018 21:08:59 - GMT works because no time is specified
// specific time echo date_i18n( 'j M Y H:i:s', strtotime('1999-11-15' ) ); // Nov 15, 1999 00:11:00 echo date_i18n( 'j M Y H:i:s', strtotime('1999-11-15' ), 1 ); // Nov 15, 1999 00:11:00 - GMT does not work because the time tag is specified
#2 The functions notes
The date_i18n()
function basically behaves exactly like the normal PHP date()
function, except that it also translates things like month names and weekdays and similar into the current locale for the site. You can replace a call to date()
with a call to date_i18n()
, using the same arguments that date()
normally takes.
The date_i18n()
function also takes an additional argument, which should be used only if you’re specifying GMT (UTC) time and not a local time.
The core of WordPress includes the necessary pieces to translate months and days and so forth in the core code, so this function is one translation function which does not need a text-domain when used in plugins and themes. The translations will always be included in the core language packs.
Note that the “format”, however, is not converted to a local one if you manually specify it. If you need a localized format, then you should use get_option('date_format') if you need the format set by the user in Settings > General
, and thus one of their choosing. Alternatively, you can wrap your predefined format in __() in order to allow translators to adjust the date to the proper local format. If you do so, then you should also include a translator comment, to let the translators know what the date format is referring to and where it is used, so they can convert it accurately.
#3 Use the genitive case
If you need to use the genitive case of a month in Greek you can use (WHILE formatted for GREEK in General Settings page):
$genitive_date = date_i18n( 'l', strtotime( '29-11-2018' ) ) . ', ' . date_i18n( 'j F Y', strtotime( '29-11-2018' ) );
The above returns: Πέμπτη, 29 Νοεμβρίου 2018
Instead of:
$date = date_i18n( 'l, j F Y', strtotime( '29-11-2018' ) );
Which returns: Πέμπτη, 29 Νοέμβριος 2018
#4 Get both date and time within a single string
To do it just concatenate the two options:
$format = get_option('date_format') .' @ '. get_option('time_format'); $datetime = date_i18n( $format ); var_dump( $datetime ); // March 3, 2018 @ 7:18 am
#5 Convert another UTC time to your own timezone.
Let us suppose you are in UTC+10 timezone And want to convert another timezone to your timezone.
$timestamp_with_offset = strtotime( '2020-05-06 23:42:59' ) + get_option( 'gmt_offset', 0 ) * HOUR_IN_SECONDS; $converted_date = date_i18n( get_option( 'date_format' ), $timestamp_with_offset );
#6 Way to show your install date/time
A simple way to show the default format of your install date/time; and to see if it works well with i18n formatting is to pull both date and time using PHP:
$format = get_option('date_format') . ' ' . get_option('time_format'); $check_stamp = date_i18n( $format ); // February 25, 2022 11:36 pm
If it shows incorrectly then you can go to your Setting > General
to make good.
Changelog
Since 0.71 | Introduced. |
Since 5.3.0 | Converted into a wrapper for wp_date(). |