Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

money_format() not showing currency

Tags:

php

I'm trying to use money_format function.

<?php

$number = 1299.46;

setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number); // Outputs 1299.46

While it should print $ sign or USD ?

I'm on linux hosting.

Thanks

like image 625
Raheel Avatar asked Jun 20 '15 17:06

Raheel


1 Answers

Locales are constants defined by the operation system.

I use Ubuntu for the following example.

  1. check which locales are supported:

    locale -a
    
  2. add the locales you want (for example ru):

    sudo locale-gen ru_RU
    sudo locale-gen ru_RU.UTF-8
    
  3. run this update comand

    sudo update-locale 
    
  4. restart php fpm ( check your php version service name like soservice --status-all)

    sudo service php7.2-fpm restart
    

But be careful, because most likely you are useing Apache with "mod_php" because it is the most popular one. And this one works in multi thread environment, when setlocale() is not thread safe. Look here for details

like image 57
Yevgeniy Afanasyev Avatar answered Oct 02 '22 12:10

Yevgeniy Afanasyev