WordPress at a glance

wp_maybe_decline_date() WP 1.0

Determines if the date should be declined.

If the locale specifies that month names require a genitive case in certain formats (like 'j F Y'), the month name will be replaced with a correct form.

✈ 1 time = 0.000227s = fast | 50000 times = 0.51s = very fast | PHP 7.2.16, WP 5.2

No Hooks.

Return

String. The date, declined if locale specifies it.

Usage

wp_maybe_decline_date( $date );
$date(string) (required)
Formatted date string.

Notes

  • Global. WP_Locale. $wp_locale

Changelog

Since 4.4.0 Introduced.

Code of wp maybe decline date: wp-includes/functions.php WP 5.2.4

<?php
function wp_maybe_decline_date( $date ) {
	global $wp_locale;

	// i18n functions are not available in SHORTINIT mode
	if ( ! function_exists( '_x' ) ) {
		return $date;
	}

	/* translators: If months in your language require a genitive case,
	 * translate this to 'on'. Do not translate into your own language.
	 */
	if ( 'on' === _x( 'off', 'decline months names: on or off' ) ) {
		// Match a format like 'j F Y' or 'j. F'
		if ( @preg_match( '#^\d{1,2}\.? [^\d ]+#u', $date ) ) {
			$months          = $wp_locale->month;
			$months_genitive = $wp_locale->month_genitive;

			foreach ( $months as $key => $month ) {
				$months[ $key ] = '# ' . $month . '( |$)#u';
			}

			foreach ( $months_genitive as $key => $month ) {
				$months_genitive[ $key ] = ' ' . $month . '$1';
			}

			$date = preg_replace( $months, $months_genitive, $date );
		}
	}

	// Used for locale-specific rules
	$locale = get_locale();

	if ( 'ca' === $locale ) {
		// " de abril| de agost| de octubre..." -> " d'abril| d'agost| d'octubre..."
		$date = preg_replace( '# de ([ao])#i', " d'\\1", $date );
	}

	return $date;
}

Related Functions

From tag: date time

No comments
    Hello, !     Log In . Register