J
J
JackShcherbakov2018-03-23 13:47:49
PHP
JackShcherbakov, 2018-03-23 13:47:49

There is a problem with the encoding of the string returned by the strftime() function. How to fix?

Hello colleagues! I recently encountered the following problem:
I want to display all the names of the months in Russian. To do this, I wrote the following script:

<?php 
setlocale(LC_TIME, ''); // во 2-м аргументе "" для того, что бы использовалась локаль системы
echo strftime('%d %Bа %Y');
?>

And here is his result:
23 ����а 2018 //а надо 23 марта 2018
For some reason, the letters m, a, p and t are replaced by �. What is the problem? How to fix?
I express my deep gratitude in advance to everyone who will help
UPD:
It did not help - www.cyberforum.ru/php-beginners/thread1135357.html
Script
<?php 
setlocale(LC_TIME, 'ru_RU.UTF-8'); 
echo strftime('%d %Bа %Y');
?>

Displays
23 Marchа 2018

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
ksnk, 2018-03-23
@ksnk

Obviously, the locale will not work for anyone, even if you miraculously manage to grope for it, simply because it is required to display "March" instead of "March". So just hardcore

function toRusDate($daystr=null,$format="j F, Y г."){
    //print_r($datstr);
    if ($daystr) $daystr=strtotime($daystr);
    else $daystr=time();
    return	str_replace( //XXX: нужно проверить английские имена месяцев
        array('january','february','march','april','may','june','july',
            'august','september','october','november','december'),
        array('января','февраля','марта','апреля','мая','июня','июля',
            'августа','сентября','октября','ноября','декабря'),
        strtolower(date($format,
            $daystr)));
}

I
ilyale, 2018-03-23
@ilyale

The fact that the letters are displayed incorrectly in the browser means:
1) the browser has opened a page on which the encoding is not set correctly (something like ). If the encoding is set correctly, but the problem continues to appear, see step 2 2) the reason may be that PHP has already received information in a broken encoding and it cannot normally give it to the browser. in this case, you need to look for the reason in the source of information header('Content-Type: text/html; charset=utf-8')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question