Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad encoding when using on strftime in spanish?

Tags:

php

I'm trying to echo the date with strftime but I'm getting bad encoding on utf-8 only characters. (accented characters basically)

setlocale(LC_TIME, 'spanish');
define("CHARSET", "iso-8859-1");
echo strftime("%A, %d de %B",strtotime($row['Date']));

Is there any problem in this part of the code? Everything is encoded in utf-8 and echoing a 'á' character above it displays the character correctly.

like image 677
lisovaccaro Avatar asked May 26 '12 08:05

lisovaccaro


2 Answers

Try adding utf8_encode()

setlocale(LC_TIME, 'spanish');
define("CHARSET", "iso-8859-1");
echo utf8_encode(strftime("%A, %d de %B",strtotime($row['Date'])));
like image 165
Galen Avatar answered Sep 28 '22 17:09

Galen


I'm a bit late, but Googling around I found this post. And the answers weren't appropriate in my case. I'm experiencing the same problem as the OP, but my locale is fr_FR and everything works fine on my computer but not on the dev server.

If I add an iconv (as most people suggest when you Google this issue), it works on the dev server but not on my computer, so I needed a "bulletproof" solution that would work the same everywhere (as there is also a production server).

So, the issue here is with the setlocale, this function changes the locale on the current execution, but every locale is associated with a charset and if none is specified, it falls back to the default one of your system (I think, in my case it was falling back to ISO-8859-1 when using the fr_FR locale). You can list all available locales on your computer/server with the locale -a command. You will most likely see the locale you want, with ".UTF-8" (in my case "fr_FR.UTF-8"), that's how you must set it: setlocale('fr_FR.UTF-8');

like image 27
Naomi Avatar answered Sep 28 '22 16:09

Naomi