I
I
Ivan Panteleev2012-11-19 21:14:18
PHP
Ivan Panteleev, 2012-11-19 21:14:18

Setting locale in php under windows to work with UTF-8?

The situation is as follows:
We create a file index.phpin encoding UTF-8without BOM with the following content:

<?php
header('Content-Type: text/html; charset=UTF8');
var_dump(strftime('Число: %d, месяц: %B, день недели: %A'));
die();
?>

And run the script under Windows.
I get the following output:
Число: 19, месяц: ������, день недели: �����������
Sometimes crashes like this:
Число: 19, месяц: November, день недели: Monday
Force locale setting
setlocale(LC_ALL, 'ru_RU.UTF-8', 'Russian_Russia.65001');
also doesn't change anything.
however, if the file encoding Windows-1251is , then no problems occur.
Can someone suggest how to solve this problem?
Mandatory requirement to keep the encoding of the files UTF-8and make Windows work fine with them. This is real?
Configuration:
OS: Windows 7
PHP: 5.4.4
Apache: 2.2.13

Answer the question

In order to leave comments, you need to log in

6 answer(s)
B
burdakovd, 2012-11-19
@Yekver

They say under Windows strftime puts on the selected encoding in the locale and works in a single-byte encoding corresponding to the requested language.
It is possible, perhaps, before each similar function (switching the locale to a familiar Windows just in case), recode the input of the function into a single-byte encoding, call it, and then recode the result into utf-8.

A
Alexander Zhigunov, 2016-06-03
@id_zhigunov

setlocale(LC_ALL, "russian");
$day = strftime('%a');
$mon = strftime('%B');
$day = iconv("windows-1251", "UTF-8", $day);
$mon = iconv("windows-1251", "UTF-8", $mon);

U
Urvin, 2012-11-20
@Urvin

<?
$days = array('Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота', 'Воскресенье');
$months = array('Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь');

$nowdate = getdate();

echo 'Число: ', $nowdate['mday'], ', месяц: ', $months[$nowdate['mon']-1], ', день недели: ', $days[$nowdate['wday']-1];
?>

O
Oleg Matrozov, 2012-11-19
@Mear

And what about Windows in general? You yourself give functions to UTF-8 input and are surprised that your output is not win-1251. If you want 1251 at the output, then let's use it at the input, at least through the same iconv.

E
edogs, 2012-11-19
@edogs

Check with the browser what content_type is _really_ given by the server.
It is quite possible that some settings will override your attempt to give utf8, for example www.php.net/manual/ru/ini.core.php#ini.default-charset or some server settings. And then understand the situation, look for what and where changes.

N
Nikita Tratorov, 2015-04-22
@NikitaTratorov

Unfortunately no. Unreal. Because of the server on Windows, you will have to write a separate wrapper function for any cases of working with dates, currencies, etc.
Example

if ( stripos($u_locale, "1251") !== false ) {
  return iconv("windows-1251","utf-8", $date_str);
} elseif (stripos($u_locale, "1252") !== false) {
  return iconv("windows-1252","utf-8", $date_str);
} else {
  return $date_str;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question