Print correct date in languages other than English

Unlike in English, full date written in many other languages (i.e. in Polish) has month written in genitive case. Neither WordPress (the_time()) nor PHP itself (date() on which the_time() is built on) supports writing dates in such format, so you’ve got to help yourself. This isn’t a hard task and there are many approaches to solve this problem across Internet. Here, I present on of the solution, ready to be used both in one, single theme or across entire WordPress Network.

We need a function, that will convert basic, singular month name into genitive case. This example is the easiest one, though not much professional, as it assumes fixed input date format:

function parse_polish_date($date)
{
    $months = array
    (
        'stycznia',
        'lutego',
        'marca',
        'kwietnia',
        'maja',
        'czerwca',
        'lipca',
        'sierpnia',
        'września',
        'października',
        'listopada',
        'grudnia'
    );
      
    $year = (int)substr($date, 0, 4);  
    $month = (int)substr($date, 5, 2);
    $day = (int)substr($date, 8, 2);

    if($month >= 1 && $month < = 12) echo($day.' '.$months[$month].' '.$year.' r.');
}

To use it, call parse_polish_date(get_the_time('Y-m-d')) anywhere within The Loop.

If you want to use it in just a single site, edit your active theme and put it in any of its files. Since in most themes date is used both in post listings (index.php) and post body (single.php) it is wise to put it into some external file (or else, you’ll have to repeat function definition in both of these files). Usually themes comes with functions.php file, which holds some theme’s internal function. Seems like a good place for such method.

If you’re working with WordPress Network and thus want to have such date converting function to be available in many sites / themes, the easiest way is to wrap it into a form of plugin, install it and network enable it. You actually don’t have to wrap anything as the simplest form of WordPress plugin contains only the code (above function) plus some comments that are extracted into Plugins page, for example:

    /*
        Plugin Name: Polish Date
        Plugin URI: http://www.acrid.pl/
        Description: Displays correct Polish date, with month name in genitive case. Usage: Call <tt>parse_polish_date(get_the_time('Y-m-d')) anywhere within The Loop.
        Version: 1.0.0
        Author: acrid.pl
        Author URI: http://www.acrid.pl/
    */

But, if you’re that much lazy, you can even omit this comment part and just put function into separate file.

Leave a Reply