Max Koretskyi
Max Koretskyi

Reputation: 105517

php - how can I output date in russian locale

I have the following date: 18/07/2013. I want it to be output in the following format 18 July 2013, where July should be replaced with Russian name of the month. So here is what I do:
1) Make timestamp out of the string 18/07/2013.
2) Set russian locale setlocale(LC_ALL, 'ru_RU.utf-8');
3) Format time using strftime("%d %B %Y", date);
Yet it outputs 18 July 2013 without substituting July with Russian name for the month. What's wrong?

Upvotes: 0

Views: 862

Answers (2)

SenseException
SenseException

Reputation: 1075

If Intl extension is installed you can use the MessageFormatter doing your format and possible grammar differences

Example: http://php.budgegeria.de/zrffntr-sbeznggre

PHP Doc: http://www.php.net/manual/en/class.messageformatter.php

If you don't need whole sentences you also can use the IntlDateFormatter: http://www.php.net/manual/en/intldateformatter.format.php

Upvotes: 0

jvicab
jvicab

Reputation: 286

What I did to achieve the same result in Spanish was define two arrays, $patterns for the names in English and $replacements with equivalent names in Spanish and call preg_replace function passing them as first and second parameters and the result of formatted date as the third parameter.

This is the function I built:

function MonthToSpanish($str, $three = false)
{
    $months = array(
        '/January/', '/February/', '/March/',
        '/April/', '/May/', '/June/', '/July/',
        '/August/', '/September/', '/October/', '/November/', '/December/',
        '/Jan/', '/Apr/', '/Aug/', '/Dec/',
    );
    $meses = array(
        'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio',
        'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre',
        'Ene', 'Abr', 'Ago', 'Dic',
    );
    $res = preg_replace($months, $meses, $str);
    if ($three) {
        $res = str_replace('Mayo', 'May', $res); 
    }
    return $res;
}

Hope you'll find it sort of useful.

Upvotes: 1

Related Questions