wp_maybe_decline_date()
Declines or modifies the month in the provided date string. For example for Russian: 15 Май > 15 мая.
Works only for languages where months in the date are declined. Whether the month in the date is declined or not is determined by the following translation line:
'on' === _x( 'off', 'decline months names: on or off' )
The function checks the provided date and if it is in the format: int word ... (15 Май, 15 май 2019 год), then the date will be processed, and the name of the month will be replaced with the name in the genitive case. Thus, for example, 15 Май 2019 will turn into 15 мая 2019.
The function does nothing (returns the provided date) if the months in the language are not declined.
By default in WP the function is hooked to date_i18n:
add_filter( 'date_i18n', 'wp_maybe_decline_date' );
The function accesses the global variable $wp_locale, which contains an object of the class WP_Locale, which has properties: month and month_genitive. It performs transformations based on these properties (case insensitive):
[month] => Array ( [01] => Январь [02] => Февраль [03] => Март [04] => Апрель [05] => Май [06] => Июнь [07] => Июль [08] => Август [09] => Сентябрь [10] => Октябрь [11] => Ноябрь [12] => Декабрь ) [month_genitive] => Array ( [01] => января [02] => февраля [03] => марта [04] => апреля [05] => мая [06] => июня [07] => июля [08] => августа [09] => сентября [10] => октября [11] => ноября [12] => декабря )
No Hooks.
Returns
String. The modified date if the site's language implies declension of the date and the provided date (unchanged) otherwise.
Usage
wp_maybe_decline_date( $date );
- $date(string) (required)
- Formatted time.
Examples
#1 Date conversion examples
This will work only if site language date parts can be declined. This option is set in translation string 'on' === _x( 'off', 'decline months names: on or off' ).
For example, suppose our site is running in the Russian language, where months could be declined, then:
echo wp_maybe_decline_date( '15 Май 2019' ); //> 15 мая 2019
The date has the right format, but the month is already written in it correctly - the function does not change anything, although it does all the same operations to find a replacement.
echo wp_maybe_decline_date( '15 Мая 2019' ); //> 15 Мая 2019 echo wp_maybe_decline_date( '15 мая 2019' ); //> 15 мая 2019
The date has the wrong format, the function just returns it without any attempt to change anything.
echo wp_maybe_decline_date( 'Май 2019 года' ); //> Май 2019 года
Notes
- Global. WP_Locale.
$wp_localeWordPress date and time locale object.
Changelog
| Since 4.4.0 | Introduced. |
| Since 5.4.0 | The $format parameter was added. |