Uma área de pesquisa sobre o WordPress para devs e autores do tema



wp_date ›

Desde5.3.0
Obsoleton/a
wp_date ( $format, $timestamp = null, $timezone = null )
Parâmetros: (3)
  • (string) $format PHP date format.
    Required: Yes
  • (int) $timestamp Optional. Unix timestamp. Defaults to current time.
    Required: No
    Padrão: null
  • (DateTimeZone) $timezone Optional. Timezone to output result in. Defaults to timezone from site settings.
    Required: No
    Padrão: null
Retorna:
  • (string|false) The date, translated if locale specifies it. False on invalid timestamp input.
Definido em:
Codex:

Retrieves the date, in localized format.

This is a newer function, intended to replace date_i18n() without legacy quirks in it.

Note that, unlike date_i18n(), this function accepts a true Unix timestamp, not summed with timezone offset.



Funções relacionadas: wp_die, wp_checkdate, is_date, wp_salt, wp_autosave

Fonte

function wp_date( $format, $timestamp = null, $timezone = null ) {
	global $wp_locale;

	if ( null === $timestamp ) {
		$timestamp = time();
	} elseif ( ! is_numeric( $timestamp ) ) {
		return false;
	}

	if ( ! $timezone ) {
		$timezone = wp_timezone();
	}

	$datetime = date_create( '@' . $timestamp );
	$datetime->setTimezone( $timezone );

	if ( empty( $wp_locale->month ) || empty( $wp_locale->weekday ) ) {
		$date = $datetime->format( $format );
	} else {
		// We need to unpack shorthand `r` format because it has parts that might be localized.
		$format = preg_replace( '/(?<!\\\\)r/', DATE_RFC2822, $format );

		$new_format    = '';
		$format_length = strlen( $format );
		$month         = $wp_locale->get_month( $datetime->format( 'm' ) );
		$weekday       = $wp_locale->get_weekday( $datetime->format( 'w' ) );

		for ( $i = 0; $i < $format_length; $i ++ ) {
			switch ( $format[ $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 .= $format[ $i ];

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

		$date = $datetime->format( $new_format );
		$date = wp_maybe_decline_date( $date );
	}

	/**
	 * Filters the date formatted based on the locale.
	 *
	 * @since 5.3.0
	 *
	 * @param string       $date      Formatted date string.
	 * @param string       $format    Format to display the date.
	 * @param int          $timestamp Unix timestamp.
	 * @param DateTimeZone $timezone  Timezone.
	 *
	 */
	$date = apply_filters( 'wp_date', $date, $format, $timestamp, $timezone );

	return $date;
}