I have a .php that retrieves the publish_up field and echo it. But it doesn't display the special characters such as é,è,û... Instead I get a . I have set this content="text/html; charset=utf-8" but it doesn't solve the problem.

Thanks a lot for your help!

      $oldLocale = setlocale(LC_TIME, 'en_US');
      setlocale(LC_TIME, $oldLocale);

It could be due to the encoding of the file. You can set encoding to UTF-8 by a good text editor like Notepad++ or by command line. For exmaple in *nix OS's:

iconv -f ascii -t utf-8 "test.php" -o "utf8.test.php"

Update 1

Also it could be due to your MySql encoding. You can add these lines before executing a query (in your model):

$jdb1->execute("SET NAMES 'utf8'");
$jdb1->execute("SET CHARACTER SET utf8");
$jdb1->execute("SET SESSION collation_connection = 'latin1_general_ci'");

$jdb1 is your JDatabase instance.

Update 2

If strange characters are returned use utf8_encode(strftime()) for UTF-8 characters

|improve this answer|||||
  • Thanks Fari! I have tried the updated solution but it's not working. I forgot to mention that my query also retrieves the introtext field in which I have special characters. The special characters in introtext are correctly displayed, I only have a problem with the one in publish_up. So I guess, that can't be due to the encoding of the file. – MagTun Sep 2 '14 at 16:48
  • According to your description, you can find the answer here. Use utf8_encode();. – Farahmand Sep 2 '14 at 18:41
  • This last link was the good one! Thanks so much Fari! – MagTun Sep 3 '14 at 6:21

It sounds like you don't have the correct character encoding set on your database. To find out, check the collation used in the database for publish_up. The correct collation to use depends on what language your website uses, but in general utf8_general_ci will work for most English language websites (apparently utf8_general_ci doesn't properly support all UTF8 coding , so if your version of mysql supports it you might want to use utf8mb4 instead.)

To check the database, I recommend using a program such as phpMyAdmin.

|improve this answer|||||
  • Thanks moomoochoo! I have try to set the the publish_up collation to utf8_general_ci (same collation I used for introtext in which I have special characters) but the database seems to not be able to process the change. Is it because the field type is DATETIME (instead of mediumtext for introtex)? I am afraid I can't change the field type for publish_up to mediumtext without seriously interfering with the Joomla structure. – MagTun Sep 2 '14 at 16:10
  • I forgot to mention: publish_up collation is empty – MagTun Sep 2 '14 at 16:49
  • OK. I wouldn't change the field type. I'd glossed over the fact that you are using DATETIME for publish_up, so my solution isn't helpful in your case. I think that Fari's link is on the right track. – TryHarder Sep 3 '14 at 0:46

Based on Fari's link (Update 2) here is the solution:

<?php $oldLocale = setlocale(LC_TIME, 'fr_FR');
$date_string = utf8_encode(strftime('%d %B %Y', strtotime($jitem1->publish_up)));
echo $date_string;
setlocale(LC_TIME, $oldLocale); 
|improve this answer|||||

Your Answer

