Un motore di ricerca WordPress per sviluppatori e autori di temi



date_i18n ›

Da0.71
Deprecaton/a
date_i18n ( $dateformatstring, $timestamp_with_offset = false, $gmt = false )
Parametri: (3)
  • (string) $dateformatstring Format to display the date.
    Richiesto:
  • (int|bool) $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset in seconds. Default false.
    Richiesto: No
    Default: false
  • (bool) $gmt Optional. Whether to use GMT timezone. Only applies if timestamp is not provided. Default false.
    Richiesto: No
    Default: false
Ritorna:
  • (string) The date, translated if locale specifies it.
Definito a:
Codex:

Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.

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.



Sorgenti

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

	$i = $timestamp_with_offset;

	if ( ! is_numeric( $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;
	$new_format = '';

	// We need to unpack shorthand `r` format because it has parts that might be localized.
	$dateformatstring = preg_replace( '/(?<!\\\\)r/', DATE_RFC2822, $dateformatstring );

	/*
	 * 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', $i );
	$gmt_mode   = $gmt && ( false === $timestamp_with_offset );
	$timezone   = $gmt_mode ? new DateTimeZone( 'UTC' ) : wp_timezone();
	$datetime   = date_create( $local_time, $timezone );

	/*
	 * 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.
	 */
	$timestamp_mode = ( 'U' === $dateformatstring );

	if ( $timestamp_mode ) {
		$new_format = $i;
	}

	if ( ! $timestamp_mode && ! empty( $wp_locale->month ) && ! empty( $wp_locale->weekday ) ) {
		$month   = $wp_locale->get_month( $datetime->format( 'm' ) );
		$weekday = $wp_locale->get_weekday( $datetime->format( 'w' ) );

		$format_length = strlen( $dateformatstring );

		for ( $i = 0; $i < $format_length; $i ++ ) {
			switch ( $dateformatstring[ $i ] ) {
				case 'D':
					$new_format .= backslashit( $wp_locale->get_weekday_abbrev( $weekday ) );
					break;
				case 'F':
					$new_format .= backslashit( $month );
					break;
				case 'l':
					$new_format .= backslashit( $weekday );
					break;
				case 'M':
					$new_format .= backslashit( $wp_locale->get_month_abbrev( $month ) );
					break;
				case 'a':
					$new_format .= backslashit( $wp_locale->get_meridiem( $datetime->format( 'a' ) ) );
					break;
				case 'A':
					$new_format .= backslashit( $wp_locale->get_meridiem( $datetime->format( 'A' ) ) );
					break;
				case '\\':
					$new_format .= $dateformatstring[ $i ];

					// If character follows a slash, we add it without translating.
					if ( $i < $format_length ) {
						$new_format .= $dateformatstring[ ++$i ];
					}
					break;
				default:
					$new_format .= $dateformatstring[ $i ];
					break;
			}
		}
	}

	$j = $datetime->format( $new_format );

	/**
	 * 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          A sum of Unix timestamp and timezone offset in seconds.
	 * @param bool   $gmt        Whether to use GMT timezone. Only applies if timestamp was
	 *                           not provided. Default false.
	 */
	$j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt );

	return $j;
}