Use setlocale for date only

Asked

Viewed 938 times

1

Well I’m using the setlocale for php to return me the name of the month in PT-BR. The problem is affecting the system calculations. php is putting a comma on the dot link.

setlocale(LC_ALL, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');

echo strftime('%b', strtotime("-1 month"));

echo "<br><br>";

echo 123.33 - 111.11;

In the example I posted he returns to me:

Mai

12,22

Since he has to return me:

Mai

12.22

How do I fix it?

  • I don’t work with PHP and may be wrong, but looking at documentation I think it’s just changing the LC_ALL for LC_TIME

  • Where do these numbers come from? You can use the function $suaVariable=str_replace(",","." ,$suaVariable);

  • I edited the answer of your previous question to remedy this problem see in https://answall.com/questions/210453/fun%C3%A7%C3%A3o-date-returning-m%C3%Aas-en/210456#210456

1 answer

2


Following the commenting of Leandro Godoy Rosa, according to documentation, there is:

  • LC_ALL for all of the Below
  • LC_COLLATE for string comparison, see strcoll()
  • LC_CTYPE for Character Classification and Conversion, for example strtoupper()
  • LC_MONETARY for localeconv()
  • LC_NUMERIC for decimal separator (See also localeconv())
  • LC_TIME for date and time formatting with strftime()
  • LC_MESSAGES for system Responses (available if PHP was Compiled with libintl)

That is, when using LC_ALL will be affecting all the items described, including the LC_NUMERIC, that defines the formatting of numbers. If you want only the affected Sefa date setting, just change to LC_TIME:

setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
  • What is wrong? https://ideone.com/RVrqJL

  • @Leocaracciolo Ideone does not allow alteration of setlocale. Test locally and you’ll see it working perfectly.

  • Thanks, I tested it locally. One more piece of information for my knowledge base that ideone does not allow changing the setlocale. That’s why I didn’t put it in my answer.

Browser other questions tagged

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