Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ru_RU" (russian) setlocale not working on date and time

Tags:

php

locale

I am trying to use Russian language with setlocale:

setlocale(LC_TIME,"ru_RUS.utf8");
echo strftime("%A, %B %d", time());

Output is : Thursday, August 29

Expected is : четверг, Август 29

Any help would be highly appreciated.

like image 934
Ravi Sharma Avatar asked Aug 29 '13 14:08

Ravi Sharma


3 Answers

Found it! if you are using Linux hosting then try:

setlocale(LC_ALL, 'ru_RU.UTF-8');

will works fine. In case you are using windows hosting then try:

<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
setlocale(LC_ALL, 'russian');
like image 80
Ravi Sharma Avatar answered Oct 23 '22 17:10

Ravi Sharma


For the Russian locale and UTF-8 it is possible to use such code. Work in Widows and Unix.

header('Content-type: text/html; charset=utf-8');

$locale_time = setlocale (LC_TIME, 'ru_RU.UTF-8', 'Rus');

function strf_time($format, $timestamp, $locale)
{
    $date_str = strftime($format, $timestamp);
    if (strpos($locale, '1251') !== false)
    {
        return iconv('cp1251', 'utf-8', $date_str);
    }
    else
    {
        return $date_str;
    }
}

echo strf_time("%A, %B %d", time(), $locale_time);

Result:

вторник, Октябрь 13
like image 4
Visman Avatar answered Oct 23 '22 17:10

Visman


var_dump(setlocale(LC_ALL, 'ru_RU.utf8'));

The function setlocale returns the result of the system call. I think it should be RU, not Ru.

like image 2
Joop Eggen Avatar answered Oct 23 '22 18:10

Joop Eggen