The date_i18n()
WordPress PHP function retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds. The function translates things like month names and weekdays into the current locale for the site. It is similar to the PHP date()
function but it also includes the translation of month and day names.
Usage
date_i18n( $format, $timestamp_with_offset, $gmt );
In this example, $format
is the format to display the date, $timestamp_with_offset
is a sum of Unix timestamp and timezone offset in seconds and $gmt
indicates whether to use GMT timezone.
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: false$gmt (bool)
: Optional. Whether to use GMT timezone. Only applies if timestamp is not provided. Default: false
More information
See WordPress Developer Resources: date_i18n()
Please note that since WordPress 5.3.0, there is also a wp_date()
function.
Examples
Display Current Date and Time
To display the current date and time, you can use date_i18n()
with the WordPress options date_format
and time_format
.
// Get current timestamp $current_timestamp = current_time('timestamp'); // Display the date and time echo date_i18n(get_option('date_format') . ' @ ' . get_option('time_format'), $current_timestamp);
Display Date for a Specific Timestamp
To display a date for a specific Unix timestamp:
// Unix timestamp $timestamp = 1620118800; // Display the date echo date_i18n('F j, Y', $timestamp);
Display Localized Date with Timezone Offset
To display a date with a timezone offset:
// Date with timezone offset $date_with_offset = strtotime('2023-05-01 00:00:00') + get_option( 'gmt_offset', 0 ) * HOUR_IN_SECONDS; // Display the date echo date_i18n('F j, Y', $date_with_offset);
Convert UTC Time to Local Timezone
To convert a UTC time to your own timezone:
// UTC time $utc_time = '2023-05-01 00:00:00'; // Convert to local timezone $local_time = date_i18n(get_option('date_format'), strtotime(get_date_from_gmt($utc_time)));
Display the Date in Genitive Case for Greek
For Greek language, to display the date in genitive case:
$genitive_date = date_i18n('l', strtotime('29-11-2023')) . ', ' . date_i18n('j F Y', strtotime('29-11-2023'));