I thought I would release my currency converter to the unsuspecting public, as it is quite useful, and also for people to critique.
Script uses the
following XML document to update a local cache file periodically. All currencies in that XML document are against the Euro (€).
The cache expiration can be modified by changing the class constant
CACHE_TIMEOUT, which is an integer representing the hours. The cache file's location can also be changed by altering the two cache constants
CACHE_NAME, and its path as set in
CACHE_DIR.
I have created the class a singleton and so all calls to the class must first pass through the
getInstance method.
A simple example on how to use the class immediately can be seen by doing the following:
php Code:
printf('Conversion: £%01.2f', TalkPHP_Currency::
getInstance()->
getConvertedAmount(100,
'USD',
'GBP'));
Any invalid currencies specified in the second or third argument will throw an exception like the following, notifying you of valid currencies:
Quote:
|
Fatal error: Uncaught exception 'Exception' with message 'Currency "USDN/A" is invalid. Available: USD, JPY, BGN, CZK, DKK, EEK, GBP, HUF, LTL, LVL, PLN, RON, SEK, CHF, NOK, HRK, RUB, TRY, AUD, BRL, CAD, CNY, HKD, IDR, INR, KRW, MXN, MYR, NZD, PHP, SGD, THB, ZAR' in C:\wamp\www\Currency.php:166 Stack trace: #0 C:\wamp\www\Currency.php(182): TalkPHP_Currency->_validateCurrency(Array) #1 C:\wamp\www\Currency.php(230): TalkPHP_Currency->getConvertedAmount(100, 'USDN/A', 'GBP') #2 {main} thrown in C:\wamp\www\Currency.php on line 166
|
The arguments should be read in the following order:
(Amount to convert, source currency, target currency).
You can check if you're currently using the cache by calling the
isUsingCache method. This will return a boolean to let you know if the cache has been used for that particular call.
To return every single conversion rate against the Euro (€) there is the
getConversionRates method. There is also a more specific function for returning a single currency's value against the Euro (€) --
getConversionRate, which accepts the currency abbreviation (USD, GBP, etcetera...).
In the downloadable file there are examples at the bottom of the document for help in using this class. However, it is fairly straightforward as it is, and really shouldn't cause anybody any issues. Just download and begin to convert currencies to other currencies immediately!
This is the comprehensive list of supported currencies (Both to and from):
- United States Dollar (USD)
- Japanese Yen (JPY)
- Bulgarian Lev (BGN)
- Czech Koruna (CZK)
- Danish Krone (DKK)
- Estonian Kroon (EEK)
- Great British Pound (GBP)
- Hungarian Forint (HUF)
- Lithuanian Litas (LTL)
- Latvian Lats (LVL)
- Polish Zloty (PLN)
- Romanian Lei (RON)
- Swedish Krona (SEK)
- Swiss Franc (CHF)
- Norwegian Krone (NOK)
- Croatian Kuna (HRK)
- Russian Ruble (RUB)
- Turkish Lira (TRY)
- Australian Dollar (AUD)
- Brazil Real (BRL)
- Canadian Dollar (CAD)
- Chinese Yuan (CNY)
- Hong Kong Dollar (HKD)
- Indonesian Rupiah (IDR)
- Indian Rupee (INR)
- South Korean Won (KRW)
- Mexican Peso (MXN)
- Malaysian Ringgit (MYR)
- New Zealand Dollar (NZD)
- Philippine Peso (PHP)
- Singapore Dollar (SGD)
- Thai Baht (THB)
- South African Rand (ZAR)
Incidentally, notice there's a currency with the abbreviation "
PHP" (Philippine Peso)

!