返回介绍

date_i18n()

发布于 2017-09-10 22:01:25 字数 8409 浏览 923 评论 0 收藏 0

date_i18n( string $dateformatstring,  bool|int $unixtimestamp = false,  bool $gmt = false )

Retrieve the date in localized format, based on timestamp.


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.


参数

$dateformatstring

(string) (Required) Format to display the date.

$unixtimestamp

(bool|int) (Optional) Unix timestamp.

Default value: false

$gmt

(bool) (Optional) Whether to use GMT timezone.

Default value: false


返回值

(string) The date, translated if locale specifies it.


源代码

File: wp-includes/functions.php

function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
	global $wp_locale;
	$i = $unixtimestamp;

	if ( false === $i ) {
		$i = current_time( 'timestamp', $gmt );
	}

	/*
	 * Store original value for language with untypical grammars.
	 * See https://core.trac.wordpress.org/ticket/9396
	 */
	$req_format = $dateformatstring;

	if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
		$datemonth = $wp_locale->get_month( date( 'm', $i ) );
		$datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
		$dateweekday = $wp_locale->get_weekday( date( 'w', $i ) );
		$dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
		$datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) );
		$datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) );
		$dateformatstring = ' '.$dateformatstring;
		$dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );

		$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
	}
	$timezone_formats = array( 'P', 'I', 'O', 'T', 'Z', 'e' );
	$timezone_formats_re = implode( '|', $timezone_formats );
	if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) {
		$timezone_string = get_option( 'timezone_string' );
		if ( $timezone_string ) {
			$timezone_object = timezone_open( $timezone_string );
			$date_object = date_create( null, $timezone_object );
			foreach ( $timezone_formats as $timezone_format ) {
				if ( false !== strpos( $dateformatstring, $timezone_format ) ) {
					$formatted = date_format( $date_object, $timezone_format );
					$dateformatstring = ' '.$dateformatstring;
					$dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring );
					$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
				}
			}
		}
	}
	$j = @date( $dateformatstring, $i );

	/**
	 * Filters the date formatted based on the locale.
	 *
	 * @since 2.8.0
	 *
	 * @param string $j          Formatted date string.
	 * @param string $req_format Format to display the date.
	 * @param int    $i          Unix timestamp.
	 * @param bool   $gmt        Whether to convert to GMT for time. Default false.
	 */
	$j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt );
	return $j;
}

更新日志

Versiondescription
0.71Introduced.

相关函数

Uses

  • wp-includes/formatting.php: backslashit()
  • wp-includes/functions.php: date_i18n
  • wp-includes/functions.php: current_time()
  • wp-includes/class-wp-locale.php: WP_Locale::get_month()
  • wp-includes/class-wp-locale.php: WP_Locale::get_month_abbrev()
  • wp-includes/class-wp-locale.php: WP_Locale::get_meridiem()
  • wp-includes/class-wp-locale.php: WP_Locale::get_weekday()
  • wp-includes/class-wp-locale.php: WP_Locale::get_weekday_abbrev()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_option()
  • Show 5 more uses Hide more uses

Used By

  • wp-admin/includes/class-wp-community-events.php: WP_Community_Events::format_event_data_time()
  • wp-admin/includes/misc.php: heartbeat_autosave()
  • wp-admin/includes/dashboard.php: wp_dashboard_recent_posts()
  • wp-admin/includes/ajax-actions.php: wp_ajax_date_format()
  • wp-admin/includes/ajax-actions.php: wp_ajax_time_format()
  • wp-admin/includes/ajax-actions.php: wp_ajax_wp_fullscreen_save_post()
  • wp-admin/includes/revision.php: wp_prepare_revisions_for_js()
  • wp-admin/includes/meta-boxes.php: post_submit_meta_box()
  • wp-admin/includes/meta-boxes.php: attachment_submit_meta_box()
  • wp-includes/general-template.php: wp_get_archives()
  • wp-includes/functions.php: mysql2date()
  • wp-includes/widgets.php: wp_widget_rss_output()
  • wp-includes/post-template.php: wp_post_revision_title()
  • wp-includes/post-template.php: wp_post_revision_title_expanded()
  • Show 9 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 5You must log in to vote on the helpfulness of this note Contributed by Samuel Wood (Otto)

    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.

  2. Depending on your blog settings you will see the date displayed in your local format, for example: 15. november 1976.

    
    <?php echo date_i18n( get_option( 'date_format' ), strtotime( '11/15-1976' ) ); ?>
     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文