date_i18n
函数
date_i18n ( $format, $timestamp_with_offset = false, $gmt = false )
- 参数
-
-
(string)
$format
Format to display the date.- Required: 是
-
(int|bool)
$timestamp_with_offset
Optional. A sum of Unix timestamp and timezone offset in seconds. Default false.- Required: 否
- Default: false
-
(bool)
$gmt
Optional. Whether to use GMT timezone. Only applies if timestamp is not provided. Default false.- Required: 否
- Default: false
-
(string)
- 返回值
-
- (string) The date, translated if locale specifies it.
- 定义位置
-
-
wp-includes/functions.php
, line 175
-
wp-includes/functions.php
- 引入
- 0.71
- 弃用
- –
检索本地化格式的日期,基于Unix时间戳和时区偏移量(秒)的总和。
如果locale指定了locale的月份和星期,那么locale将接管日期的格式。如果不是这样,那么日期格式字符串将被替代使用。
请注意,由于WP通常用`strtotime()`生成时间戳和偏移量的方式,它意味着偏移量是在_当前_时间添加的,而不是在时间戳代表的时间。存储这样的时间戳或以不同的方式计算它们将导致无效的输出。
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 ) ) { // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested $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; }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。