wp_maybe_decline_date( string $date, string $format = '' ): string
- Since
- 4.4.0, 5.4.0
- Source
wp-includes/functions.php:338
Description
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.
Compatibility
- WordPress
- since 5.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$datestring- Formatted date string.
$formatstringoptional- Date format to check. Default empty string.Default:
''
Return value
string- The date, declined if locale specifies it.
Performance profile
How much work a call to wp_maybe_decline_date() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 8–50
- Plugin surface
- None
- Called by
- 2
Reads stored settings via get_site_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 84.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_maybe_decline_date() - optionnetwork option
get_site_option()one call below wp_maybe_decline_date()
Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_maybe_decline_date() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!function_exists() | 8 | function_exists() |
function_exists() | 19–50 | function_exists(), _x(), get_locale() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 84 | 8–50 | 15 | |
| 8.5 | 84 | 8–50 | 15 | |
| 8.4 | 84 | 8–50 | 15 | 23 fewer instructions than PHP 8.3 |
| 8.3 | 107 | 8–67 | 15 | |
| 8.2 | 107 | 8–67 | 15 | |
| 8.1 | 107 | 8–67 | 15 | |
| 7.4 | 107 | 8–67 | 15 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 2
- _x()Retrieves translated string with gettext context.
- get_locale()Retrieves the current locale.
Used by · 2
- WP_Community_Events::format_event_data_time()Adds formatted date and time items for each event in an API response.
- wp_date()Retrieves the date, in localized format.
Source code
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;}Changelog
Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$format parameter was added.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.