WordPress函数:date_i18n 日期国际化

江河/ 04月21日/ WordPress/ 浏览 150

函数原型:


date_i18n( string $format, int|bool $timestamp_with_offset = false, bool $gmt = false ): string


根据Unix时间戳和时区偏移量(以秒为单位)的总和,以本地化格式检索日期。


如果区域设置指定了区域设置月份和工作日,则区域设置将接管日期的格式。如果不是,则将使用日期格式字符串。


请注意,由于WP通常使用strtotime()生成时间戳和偏移量的总和,这意味着偏移量是在当前时间添加的,而不是在时间戳所代表的时间添加的。存储这样的时间戳或以不同的方式计算它们将导致无效的输出。


参数说明:


$format 格式化以显示日期。


$timestamp_with_offset Unix时间戳和时区偏移量的总和(以秒为单位)。


$gmt 是否使用GMT时区。仅在未提供时间戳的情况下适用。


函数源码:


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 );
	}

	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 {
		$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 );
	}

	$date = apply_filters( 'date_i18n', $date, $format, $timestamp, $gmt );

	return $date;
}


包含钩子:


apply_filters( ‘date_i18n’, string $date, string $format, int $timestamp, bool $gmt )


使用举例:


$dt_gmt = '2018-12-03 00:00:00';

echo 'date_i18n GMT, gmt=false: ' . date_i18n($settings_datetime_format, strtotime($dt_gmt)) . '<br>';
echo 'date_i18n GMT, gmt=true: ' . date_i18n($settings_datetime_format, strtotime($dt_gmt), true) . '<br>';


发表评论

暂无评论,抢个沙发...

客服 工单