Skip to content

Instantly share code, notes, and snippets.

Created May 27, 2024 11:42
Show Gist options
  • Save s-a-s-k-i-a/cb25a8d354af5b61ad8b7c69d4bb91c8 to your computer and use it in GitHub Desktop.
Save s-a-s-k-i-a/cb25a8d354af5b61ad8b7c69d4bb91c8 to your computer and use it in GitHub Desktop.
Custom WordPress date_i18n function for usage with finnish language
* Customizes date output to support Finnish nominative, genitive, and partitive cases based on format.
* This function adjusts the Finnish month names according to their grammatical cases in date formats.
* It supports:
* - Partitive case when the day precedes the month (e.g., "1. tammikuuta").
* - Genitive case when the month precedes the day (e.g., "tammikuun 1.").
* The function defaults to nominative case if the format does not match the above patterns.
* @param string $format The date format string. Supports 'j F, Y' for partitive and 'F j, Y' for genitive.
* @param int|null $timestamp Optional. Unix timestamp. Defaults to current time if null.
* @return string The formatted date string according to the specified format and Finnish grammatical rules.
function fi_date_i18n($format, $timestamp = null) {
// Use current time if no timestamp is provided.
if (null === $timestamp) {
$timestamp = current_time('timestamp');
// Define Finnish month names in nominative, genitive, and partitive cases.
$month_names_nominative = [
'tammikuu', 'helmikuu', 'maaliskuu', 'huhtikuu', 'toukokuu',
'kesäkuu', 'heinäkuu', 'elokuu', 'syyskuu', 'lokakuu',
'marraskuu', 'joulukuu'
$month_names_genitive = [
'tammikuun', 'helmikuun', 'maaliskuun', 'huhtikuun', 'toukokuun',
'kesäkuun', 'heinäkuun', 'elokuun', 'syyskuun', 'lokakuun',
'marraskuun', 'joulukuun'
$month_names_partitive = [
'tammikuuta', 'helmikuuta', 'maaliskuuta', 'huhtikuuta', 'toukokuuta',
'kesäkuuta', 'heinäkuuta', 'elokuuta', 'syyskuuta', 'lokakuuta',
'marraskuuta', 'joulukuuta'
// Extract day, month, and year from the timestamp.
$date = date('j', $timestamp);
$month = date('n', $timestamp) - 1; // Convert 1-based index to 0-based.
$year = date('Y', $timestamp);
// Determine the grammatical case based on the format string.
if (strpos($format, 'j F') !== false) {
// Use partitive case when day precedes month.
$month_name = $month_names_partitive[$month];
$formatted_date = str_replace('F', $month_name, $format);
$formatted_date = str_replace('j', $date . '.', $formatted_date); // Append period after day.
} else if (strpos($format, 'F j') !== false) {
// Use genitive case when month precedes day.
$month_name = $month_names_genitive[$month];
$formatted_date = str_replace('F', $month_name, $format);
$formatted_date = str_replace('j', $date . '.', $formatted_date); // Append period after day.
} else {
// Default to nominative case.
$month_name = $month_names_nominative[$month];
$formatted_date = str_replace('F', $month_name, $format);
$formatted_date = str_replace('j', $date, $formatted_date);
// Replace the year in the format.
$formatted_date = str_replace('Y', $year, $formatted_date);
return $formatted_date;
// echo fi_date_i18n('j F, Y', null); // "1. tammikuuta 2024"
// echo fi_date_i18n('F j, Y', null); // "tammikuun 1. 2024"
Copy link

Description of the Custom fi_date_i18n() Function

The fi_date_i18n() function is designed to address a longstanding issue with date formatting in Finnish on WordPress sites, as detailed in the WordPress Trac ticket #49012. This ticket highlights problems with the Finnish date format "F j, Y" displaying incorrectly, such as "2 joulukuun, 2019" instead of the expected "joulukuun 2, 2019". The issue arises from the incorrect application of grammatical cases in Finnish date strings, which has not been resolved in the core WordPress code even after several years.

This custom function provides a workaround by correctly formatting Finnish dates to respect the grammatical rules of the Finnish language, specifically handling the nominative, genitive, and partitive cases based on the position of the month and day in the date string. It ensures that:

  • When the day precedes the month, the month is in the partitive case (e.g., "1. tammikuuta").
  • When the month precedes the day, the month is in the genitive case (e.g., "tammikuun 1.").

Purpose of the Function

The primary purpose of this function is to provide developers and site administrators with a reliable method to display dates correctly in Finnish until the core issue is addressed in WordPress. This function can be used as a drop-in replacement or supplement to the native date_i18n() function in WordPress.
GitHub Link to file

For Developers on GitHub

For developers working on the WordPress core, especially those focusing on internationalization and localization issues, this function represents a practical interim solution to a specific localization problem. If you are involved in developing or refining WordPress's date/time component, consider reviewing the approach taken in this function. Contributions to the discussion or code that could lead to a permanent fix in WordPress core would be highly beneficial. This could involve refining regex patterns for word boundaries in date strings or adjusting the wp_maybe_decline_date() function to handle Finnish and other languages with unique grammatical rules more effectively. @Rarst

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment