lisovaccaro
lisovaccaro

Reputation: 33946

Bad encoding when using on strftime in spanish?

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.

Upvotes: 9

Views: 8342

Answers (4)

Naomi
Naomi

Reputation: 381

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');

Upvotes: 11

Timo Huovinen
Timo Huovinen

Reputation: 55613

For those that don't have iconv, you can use the mb function to convert the stftime encoded string to utf-8

echo mb_convert_encoding(strftime("%A, %d de %B",strtotime($row['Date'])), 'UTF-8', mb_internal_encoding());

Upvotes: 0

user1299518
user1299518

Reputation:

perhaps:

echo iconv("iso-8859-1","utf-8",strftime("%A, %d %B",strtotime($row['Date'])));

Upvotes: 4

Galen
Galen

Reputation: 30170

Try adding utf8_encode()

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

Upvotes: 20

Related Questions