Reputation: 71918
I'm using setlocale
and strftime
to display localized month names in Brazilian Portuguese:
setlocale(LC_ALL, 'pt_BR');
$meses = array();
for($m=1; $m<=12; $m++) {
$meses[$m] = strftime('%B', mktime(0, 0, 0, $m, 1));
}
This works fine in my dev environment, but on the production server it fails for March (which in Portuguese is "março"). If I utf8_encode
the month names, it works on the server, but on my dev environment I get a double-encoded output. I suspect that's because of different locale encodings on each machine. I guess the proper solution would be to install a UTF8 locale setting for pt_BR on the server, but I'd like to avoid that if possible (long story short, the server admins are, er, difficult).
Is it possible to detect the encoding of the current locale from PHP, so I can code around this issue, and make my code work regardless of the locale/encoding settings?
Upvotes: 2
Views: 2371
Reputation: 55623
To get the encoding used by strftime use
mb_internal_encoding()
to convert the stftime encoded string to utf-8, use
mb_convert_encoding(strftime($format, $timestamp), 'UTF-8', mb_internal_encoding());
Upvotes: -1
Reputation: 157992
In comments below the question we could resolve the problem. It was caused because the locale names differed on both systems one used pt_BR.uft-8
the other pt_BR.UTF8
.
Finally we agreed to define the locale in a config file and use that.
Upvotes: 2
Reputation: 12836
Use
echo setlocale(LC_CTYPE, 0); // en_US.UTF-8
to get the current encoding and then work around it.
Passing zero to setLocale
gives you locale values. In this case we are getting the value for LC_CTYPE
setting.
Upvotes: 3