setlocale is not working in PHP

Asked

Viewed 296 times

0

I am following the main standards to obey the format UTF-8. So above all my application code is the following.

/** Define a timezone padrão */
date_default_timezone_set( 'America/Sao_Paulo' );
/** Define o local */
setlocale( LC_ALL, 'pt_BR.utf8', 'pt_BR.utf8', 'pt_BR.utf8', 'portuguese' );

/** Habilita o encoding para UTF-8 */
mb_internal_encoding( 'UTF-8' );
mb_http_output( 'UTF-8' );
mb_regex_encoding( 'UTF-8' );

Later in one of my functions, I collect the date of the database and print it in full format, in the following procedures:

$date = DateTime::createFromFormat ( 'Y-m-d H:i:s', $this->date );
echo $date->format('d \d\e M \d\e Y');

Instead of printing out:

29 de Maio de 2017

Is printing:

29 de May de 2017

To make sure you locate it pt_BR is installed in the Ubuntu Server, I executed the following command locale -a and I got the result:

C
C.utf8
en_US.utf8
POSIX
pt_BR.utf8

I own the PHP 7.1 and Ubuntu Server 14.04.5 LTS. What might be going on?

  • I don’t know if this is the case, but I noticed that PHP 7.1 is bug in dates (at least for me). In my program I set the date and it always came out with 3 hours more, even declaring in set_locale and in the configuration file that was Brazil zone. I downgrade to the 7.0 and solved my problem

  • Is this setlocale setting in all settings? Try to create a file and put the settings then use the date format (all in the same file). See if the problem still continues.

  • @Brunofolle downgraded it to version 7.0 and I got it active in Apache, but the problem persists. The time is correct, the problem is even with the translation of the month, for example.

  • @Moisesgama had already done this in a separate test file and the problem persists.

  • Right, as far as I know the setlocale needs to be in a right sequence. : // DEFINE A LIGUAGEM COMO PT-BR E UTF8
 setlocale(LC_ALL, 'pt_BR', 'pt_BR.UTF-8', 'pt_BR.utf-8', 'portuguese');

 // DEFINE O LOCAL DA APLICAÇÃO COM SÃO PAULO/BRAZIL/AMERICA
 date_default_timezone_set('America/Sao_Paulo');

 $date = DateTime::createFromFormat ( 'Y-m-d H:i:s', $this->date );
 echo $date->format('d \d\e M \d\e Y');

  • @Moisesgama actually they are independent properties, so the order does not influence. But to test, I made the exchange and the problem persisted.

  • Do you have root server access? Can give SSH commands?

  • If yes, try these commands: First command: sudo locale-gen pt_BR.UTF-8 Second command: sudo dpkg-reconfigure locales

  • Then restarts Apache: service apache2 Restart

  • @Moisesgama as I already said in my report, the locale pt_BR.UTF-8 is already installed and configured in Ubuntu.

  • Okay, sorry I hadn’t seen it. I don’t know how to help. I wish you good luck.

  • Do you want to just translate the date? function date() and class DateTime are not affected by the locale.

  • Try to change the whole DateTime::createFromFormat for echo strftime('%d de %B de %Y', strtotime(date('Y-m-d')));

Show 8 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.