wp_maybe_decline_date

The timeline below displays how wordpress function wp_maybe_decline_date has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.4

/**
 * 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.
 *
 * @since 4.4.0
 * @since 5.4.0 The `$format` parameter was added.
 *
 * @global WP_Locale $wp_locale WordPress date and time locale object.
 *
 * @param string $date   Formatted date string.
 * @param string $format Optional. Date format to check. Default empty string.
 * @return string The date, declined if locale specifies it.
 */
function wp_maybe_decline_date($date, $format = '')
{
    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')) {
        $months = $wp_locale->month;
        $months_genitive = $wp_locale->month_genitive;
        /*
         * Match a format like 'j F Y' or 'j. F' (day of the month, followed by month name)
         * and decline the month.
         */
        if ($format) {
            $decline = preg_match('#[dj]\.? F#', $format);
        } else {
            // If the format is not passed, try to guess it from the date string.
            $decline = preg_match('#\b\d{1,2}\.? [^\d ]+\b#u', $date);
        }
        if ($decline) {
            foreach ($months as $key => $month) {
                $months[$key] = '# ' . preg_quote($month, '#') . '\b#u';
            }
            foreach ($months_genitive as $key => $month) {
                $months_genitive[$key] = ' ' . $month;
            }
            $date = preg_replace($months, $months_genitive, $date);
        }
        /*
         * Match a format like 'F jS' or 'F j' (month name, followed by day with an optional ordinal suffix)
         * and change it to declined 'j F'.
         */
        if ($format) {
            $decline = preg_match('#F [dj]#', $format);
        } else {
            // If the format is not passed, try to guess it from the date string.
            $decline = preg_match('#\b[^\d ]+ \d{1,2}(st|nd|rd|th)?\b#u', trim($date));
        }
        if ($decline) {
            foreach ($months as $key => $month) {
                $months[$key] = '#\b' . preg_quote($month, '#') . ' (\d{1,2})(st|nd|rd|th)?([-–]\d{1,2})?(st|nd|rd|th)?\b#u';
            }
            foreach ($months_genitive as $key => $month) {
                $months_genitive[$key] = '$1$3 ' . $month;
            }
            $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;
}

WordPress Version: 3.1

/**
 * 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.
 *
 * @since 4.4.0
 *
 * @global WP_Locale $wp_locale WordPress date and time locale object.
 *
 * @param string $date Formatted date string.
 * @return string The date, declined if locale specifies it.
 */
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')) {
        $months = $wp_locale->month;
        $months_genitive = $wp_locale->month_genitive;
        /*
         * Match a format like 'j F Y' or 'j. F' (day of the month, followed by month name)
         * and decline the month.
         */
        if (preg_match('#\b\d{1,2}\.? [^\d ]+\b#u', $date)) {
            foreach ($months as $key => $month) {
                $months[$key] = '# ' . preg_quote($month, '#') . '\b#u';
            }
            foreach ($months_genitive as $key => $month) {
                $months_genitive[$key] = ' ' . $month;
            }
            $date = preg_replace($months, $months_genitive, $date);
        }
        /*
         * Match a format like 'F jS' or 'F j' (month name, followed by day with an optional ordinal suffix)
         * and change it to declined 'j F'.
         */
        if (preg_match('#\b[^\d ]+ \d{1,2}(st|nd|rd|th)?\b#u', trim($date))) {
            foreach ($months as $key => $month) {
                $months[$key] = '#\b' . preg_quote($month, '#') . ' (\d{1,2})(st|nd|rd|th)?\b#u';
            }
            foreach ($months_genitive as $key => $month) {
                $months_genitive[$key] = '$1 ' . $month;
            }
            $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;
}

WordPress Version: 5.3

/**
 * 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.
 *
 * @since 4.4.0
 *
 * @global WP_Locale $wp_locale WordPress date and time locale object.
 *
 * @param string $date Formatted date string.
 * @return string The date, declined if locale specifies it.
 */
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')) {
        $months = $wp_locale->month;
        $months_genitive = $wp_locale->month_genitive;
        // Match a format like 'j F Y' or 'j. F'
        if (preg_match('#^\d{1,2}\.? [^\d ]+#u', $date)) {
            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);
        }
        // Match a format like 'F jS' or 'F j' and change it to 'j F'
        if (preg_match('#^[^\d ]+ \d{1,2}(st|nd|rd|th)? #u', trim($date))) {
            foreach ($months as $key => $month) {
                $months[$key] = '#' . $month . ' (\d{1,2})(st|nd|rd|th)?#u';
            }
            foreach ($months_genitive as $key => $month) {
                $months_genitive[$key] = '$1 ' . $month;
            }
            $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;
}

WordPress Version: 4.9

/**
 * 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.
 *
 * @since 4.4.0
 *
 * @global WP_Locale $wp_locale
 *
 * @param string $date Formatted date string.
 * @return string The date, declined if locale specifies it.
 */
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;
}

WordPress Version: 4.6

/**
 * 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.
 *
 * @since 4.4.0
 *
 * @param string $date Formatted date string.
 * @return string The date, declined if locale specifies it.
 */
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;
}

WordPress Version: 4.1

/**
 * 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.
 *
 * @since 4.4.0
 *
 * @param string $date Formatted date string.
 * @return string The date, declined if locale specifies it.
 */
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}\.? \w+#u', $date)) {
            $months = $wp_locale->month;
            foreach ($months as $key => $month) {
                $months[$key] = '#' . $month . '#';
            }
            $date = preg_replace($months, $wp_locale->month_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;
}

WordPress Version: 4.4

/**
 * 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.
 *
 * @since 4.4.0
 *
 * @param string $date Formatted date string.
 * @return string The date, declined if locale specifies it.
 */
function wp_maybe_decline_date($date)
{
    global $wp_locale;
    /* 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}\.? \w+#u', $date)) {
            $months = $wp_locale->month;
            foreach ($months as $key => $month) {
                $months[$key] = '#' . $month . '#';
            }
            $date = preg_replace($months, $wp_locale->month_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;
}