![]() |
Creating a Simple Currency Converter with Automatic Symbols
1 Attachment(s)
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:
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:
PHP Code:
PHP Code:
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:
PHP Code:
PHP Code:
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:
Quote:
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:
PHP Code:
PHP Code:
|
This is a good post :), I dugg it, to be correct are you adam on digg :D?
|
That be me :) !
|
Very nice tutorial there Adam. :)
|
excellent tutorial! was looking for something exactly like this.
__________________ Save YouTube Videos |
I'm glad you liked it, Germanium :-) I do hope you stick with us here at TalkPHP!
|
I don't need this now, but could see this being useful in the future.
Thanks! ;) |
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. |
this looks good - but it seems static? or can you automatically grab the latest conversion rates on a daily basis?
|
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?
|
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)' |
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 .
|
| All times are GMT. The time now is 09:39 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0