TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (10) Thread Tools Search this Thread Display Modes
Old 11-09-2007, 03:30 PM   10 links from elsewhere to this Post. Click to view. #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Big Grin Creating a Simple Currency Converter with Automatic Symbols

If you're from the UK then you'll appreciate this preface. If you're not then don't worry because I'll be doing the script from a USD perspective to satisfy the majority! Today we'll be making a currency converter, and as is common on Blue Peter, I'll be throwing bits of code at you like - here's one I made earlier! Basically what we'll be creating is a converter that automatically prepends the currency symbol to the front of the value and then works out the value in the currency that we specify.

We're going to begin this article with three nice defines. These defines will represent three different locales: Canada, Germany and Great Britain. There's no need at all to specify the United States because we will be converting from the USD to other currencies. Don't worry though, I will also be teaching you how to convert from another to currency to the USD with one or two amendments.

PHP Code:
define('CURRENCY_CAD''english-can');
define('CURRENCY_EUR''german');
define('CURRENCY_GBP''english-uk'); 
A comprehensive list of county codes can be found in the ISO 639 document and Microsoft MSDN also has a page on it. As my developer machine is on Windows, I will be using Windows for these but adding and editing language codes is rather simple. Unix even seems to support the above language codes when I tested the script.

All that these defines specify are the language codes to be used for various currencies which will allow us to automatically procure the currency symbol later on in our script. Once we have set out our series of defines for our supported currencies, we can begin with our function. I hope nobody has any problems with me calling the function get_currency. Seems logical! We're going to give it two arguments: one to specify the amount to convert and a second to set the particular currency we wish to convert to:

PHP Code:
function get_currency($iPrice$szLocale
Now what we have to do is configure the exchange rates that we wish to use. We are going to tell the script how much we can get for 1 precious US Dollar (USD). I will be using XE.com's Universal Currency Converter to calculate how much we can get for 1 dollar in the 3 currencies we are supporting: CDN, EUR and GBP. Once we have this information we can construct our array inside our function. Technically, you can construct the array outside of the function and then simply make it global, if you wish to.

PHP Code:
$aExchangeRates = array    (    
                            
'CAD' => 0.919996,
                            
'EUR' => 0.681642,
                            
'GBP' => 0.475255
                        
); 
Whether you know it or not, we now have all the information to begin with the core of the function. This will be where we procure the currency's symbol and then calculate the amount based on the exchange rates we specified from XE.com.

PHP Code:
setlocale(LC_MONETARY$szLocale);
$aLocale localeconv(); 
First we set locale to the region we wish to convert the USD amount to. These are located in the three defines at the beginning of our script. As we only need the monetary side of things, we explicitly tell PHP to change only that - this is done via the LC_MONETARY constant. Once we have changed the monetary locale to our desired location, we can acquire the currency's information - items such as its symbol ($, €, £, etc.) and international symbol (USD, EUR, GBP, etc.). We can use the international symbol to automatically obtain the conversion rate from our array that we constructed earlier. This is done like so:

PHP Code:
$iExchangeRate $aExchangeRates[trim($aLocale['int_curr_symbol'])]; 
With the above line we have obtained the current exchange rate that we input into our array. The next line is the heart of the conversion which is a simple mathematical multiplication:

PHP Code:
$iTotal $iPrice $iExchangeRate
There's really nothing that needs explaining in the above. We've simply multiplied the USD amount we fed into the function by the amount of the target currency you can get for 1 dollar. All we do then is return the amount and prepend the target currency's symbol:

PHP Code:
return $aLocale['currency_symbol'] . round($iTotal2); 
We have also used the round function to round the number 2 decimal places - which is how you will see almost every currency around the world formatted.

Now that we have completed our fairly straightforward function, we can make a call to it and let it convert an amount of 50 USD into, let's say, GBP:

PHP Code:
echo get_currency(50CURRENCY_GBP); 
Our function then returns the converted amount and we echo it out:

Quote:
£23.76
As you can see our function has cleverly deduced that $50 USD in GBP comes to £23.76. You can see that it's even prepended the correct currency symbol for us! How handy is that?

If you're not from the United States and wish to convert from, for example, GBP to the USD, then all you have to do is change one or two things as aforementioned. We first add a new define to the top of our page, like so:

PHP Code:
define('CURRENCY_USD''american-english'); 
We then also add to our array how many US dollars we can get for 1 Great British pound using XE.com, again. XE tells us you can get 2.10260 and as I have no reason to argue with that, we'll drop it straight into our array:

PHP Code:
$aExchangeRates = array    (    
                            
'CAD' => 0.919996,
                            
'EUR' => 0.681642,
                            
'GBP' => 0.475255,
                            
'USD' => 2.10260
                        
); 
If we were then to change our function call to specify the target currency as USD, and if you remember rightly we do that like this:

PHP Code:
echo get_currency(50CURRENCY_USD); 
Now when we run that code it will correctly tell us that £50.00 GBP equals $105.13 USD. I'm sure you're beginning to see how so flexible and beautiful our currency converter function is! I do hope that you've also learned a few things along the way. Please see the attached document for the full function.
Attached Files
File Type: php CurrencyConverter-TalkPHP.php (699 Bytes, 3937 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.

Last edited by Wildhoney : 11-11-2007 at 07:29 PM.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
Alan @ CIT (01-15-2008)
Old 11-11-2007, 09:23 AM   #2 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

This is a good post :), I dugg it, to be correct are you adam on digg :D?
Nor is offline  
Reply With Quote
Old 11-11-2007, 01:34 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

That be me :) !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-11-2007, 04:40 PM   #4 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

Very nice tutorial there Adam. :)
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 11-12-2007, 05:52 AM   #5 (permalink)
The Wanderer
 
Germanium's Avatar
 
Join Date: Nov 2007
Posts: 9
Thanks: 0
Germanium is on a distinguished road
Default

excellent tutorial! was looking for something exactly like this.

__________________
Save YouTube Videos

Last edited by Germanium : 11-30-2007 at 06:16 PM.
Send a message via ICQ to Germanium Send a message via MSN to Germanium Send a message via Yahoo to Germanium
Germanium is offline  
Reply With Quote
Old 11-30-2007, 01:08 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I'm glad you liked it, Germanium I do hope you stick with us here at TalkPHP!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-30-2007, 01:10 PM   #7 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

I don't need this now, but could see this being useful in the future.

Thanks! ;)
bdm is offline  
Reply With Quote
Old 01-11-2008, 01:08 AM   #8 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 7
Thanks: 0
soup is on a distinguished road
Default Warning

This relies on the locales being set on the server

The "locale" always depends on the server configuration.

which to be honest is totally unecesary in this script with just 4 currencies.
soup is offline  
Reply With Quote
Old 09-21-2009, 11:49 AM   #9 (permalink)
The Visitor
 
Join Date: Sep 2009
Posts: 1
Thanks: 0
steveNO is on a distinguished road
Default

this looks good - but it seems static? or can you automatically grab the latest conversion rates on a daily basis?
steveNO is offline  
Reply With Quote
Old 12-01-2009, 11:47 PM   #10 (permalink)
The Visitor
 
Join Date: Dec 2009
Posts: 1
Thanks: 0
bedex78 is on a distinguished road
Default

Yes, I would also like to know how we can automatically grab the latest conversion rates on a daily basis... Is this possible? Is there any sites that provides the data for free?
bedex78 is offline  
Reply With Quote
Old 12-02-2009, 12:20 AM   #11 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

A quick search turned up this site; http://rss.timegenie.com/foreign_exchange_rates_forex

There's probably more like it, try google 'exchange rates (xml|feed|api)'
delayedinsanity is offline  
Reply With Quote
Old 03-16-2010, 05:22 PM   #12 (permalink)
The Visitor
 
Join Date: Mar 2010
Posts: 1
Thanks: 0
womers is on a distinguished road
Default Easy and Free Currency Converter API for PHP

Hey, I found a free and really easy PHP exchange rate converter at www.exchangerate-api.com , it's really easy to use, and is reliable (have been using it for the past year and no glitches) Some example code can be seen at www.exchangerate-api.com/php .
womers is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/general/1422-creating-simple-currency-converter-automatic-symbols.html
Posted By For Type Date
Linux Knowledge Base and Tutorial This thread Refback 01-13-2008 09:44 AM
- IT ITnotice.ru! This thread Refback 01-12-2008 11:18 AM
Linux Knowledge Base and Tutorial - PHP: Part 2 - Currency Converter with Automatic Conversion Rates via XML This thread Refback 01-11-2008 11:13 PM
Automatic Symbols - IT ITnotice.ru! This thread Refback 01-10-2008 10:59 AM
Automatic Symbols - Realcoding.NET This thread Refback 01-08-2008 09:03 AM
ardnet's bookmarks tagged with This thread Refback 01-07-2008 01:20 AM
Digg / Programming This thread Refback 01-06-2008 05:24 PM
- Realcoding.NET This thread Refback 12-22-2007 07:08 PM
Simple PHP Currency Converter | David Bisset: Web Designer, Coder, Wordpress Guru This thread Refback 12-22-2007 06:58 AM
Digg - PHP Walkthrough: Creating a Currency Converter with Automatic Symbols This thread Refback 12-22-2007 01:41 AM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:39 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design