date_i18n( string $format, int|bool $timestamp_with_offset = false, bool $gmt = false )
Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.
Description Description
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.
Parameters Parameters
- $format
-
(string) (Required) Format to display the date.
- $timestamp_with_offset
-
(int|bool) (Optional) A sum of Unix timestamp and timezone offset in seconds.
Default value: false
- $gmt
-
(bool) (Optional) Whether to use GMT timezone. Only applies if timestamp is not provided.
Default value: false
Return Return
(string) The date, translated if locale specifies it.
Source Source
File: wp-includes/functions.php
function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) { $timestamp = $timestamp_with_offset; // If timestamp is omitted it should be current time (summed with offset, unless `$gmt` is true). if ( ! is_numeric( $timestamp ) ) { $timestamp = current_time( 'timestamp', $gmt ); } /* * This is a legacy implementation quirk that the returned timestamp is also with offset. * Ideally this function should never be used to produce a timestamp. */ if ( 'U' === $format ) { $date = $timestamp; } elseif ( $gmt && false === $timestamp_with_offset ) { // Current time in UTC. $date = wp_date( $format, null, new DateTimeZone( 'UTC' ) ); } elseif ( false === $timestamp_with_offset ) { // Current time in site's timezone. $date = wp_date( $format ); } else { /* * Timestamp with offset is typically produced by a UTC `strtotime()` call on an input without timezone. * This is the best attempt to reverse that operation into a local time to use. */ $local_time = gmdate( 'Y-m-d H:i:s', $timestamp ); $timezone = wp_timezone(); $datetime = date_create( $local_time, $timezone ); $date = wp_date( $format, $datetime->getTimestamp(), $timezone ); } /** * Filters the date formatted based on the locale. * * @since 2.8.0 * * @param string $date Formatted date string. * @param string $format Format to display the date. * @param int $timestamp A sum of Unix timestamp and timezone offset in seconds. * Might be without offset if input omitted timestamp but requested GMT. * @param bool $gmt Whether to use GMT timezone. Only applies if timestamp was not provided. * Default false. */ $date = apply_filters( 'date_i18n', $date, $format, $timestamp, $gmt ); return $date; }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
5.3.0 | Converted into a wrapper for wp_date(). |
0.71 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
The
date_i18n()
function basically behaves exactly like the normal PHPdate()
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 todate()
with a call todate_i18n()
, using the same arguments thatdate()
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.Feedback
If you need to use the genitive case of a month in Greek you can use (WHILE formatted for GREEK in General Settings page):
The above returns:
Πέμπτη, 29 Νοεμβρίου 2018
Instead of:Which returns:
Πέμπτη, 29 Νοέμβριος 2018
— By timon33 —Depending on your blog settings you will see the date displayed in your local format, for example: 15. november 1976.
It is important to note that
date_i18n()
:date()
, not all formats are supported (such as shorthands);$gmt
argument under normal circumstances;Any use of this function must be carefully audited for correctness, especially in regards to output of time zones.
To get both the date and time within a single string, use
date_i18n
twice with a separator. At the same time, you can also retrieve the local Date and Time formats that are set within the General Settings page.For example, to return: ‘March 3, 2018 @ 7:18 am’ (formatted for the US, Eastern Standard Time in the General Settings page):
Result:
The third argument has no effect if second argument was set.
To display translated date/time based on WP settings (time zone, format). The
$unixtimestamp
argument must be convert from GMT like this.The result will be:
This is based on Thai language, Bangkok time zone.
More examples are on [moderated]
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. You can do this by adding +10 on date_18n() function.
$converted_date = date_i18n( get_option( 'date_format' ), strtotime( '2019-06-07 00:35:32' . '+11' ) )
Thanks
Feedback
Should be:
$converted_date = date_i18n( get_option( 'date_format' ), (strtotime( '2020-05-06 23:42:59' ) + get_option( 'gmt_offset', 0 ) * HOUR_IN_SECONDS) );
— By Sushil Adhikari —