An on-the-fly currency conversion can be very helpful, especially for shopping cart web sites or any industry where currency needs to translated. You can give your users a rough estimate based on local rates of what a product costs in their currency without them needing to consult a currency converter. An even better approach is to determine their currency based on what country their host is coming from.
The European Central Bank provides a free XML feed of latest rates for currencies around the world, with the Euro as the base rate. This is the information we'll be caching and parsing for current rates when we do a conversion.
You may still look up a name and symbol for any currency in your currency array. But as of this writing only the following 34 currencies can be converted:
An example that converts 50 Euros to US Dollars. Note that look-ups are case-insensitive. Although a currency can be applied to multiple countries, a country can have only currency.
/* # ! CURRENCY SEARCHES (ARRAY-BASED) ARE CASE-INSENSITIVE CAN BE FOUND IN ISO.INC.PHP # These are all valid searches: euR, euro, germany, de */ date_default_timezone_set('UTC'); require_once('currency.class.php'); $currency = new Currency('eur'); if(!empty($currency->id)) { # Found valid currency echo "<p>You are using: {$currency->name}</p>"; # You are using: Euro if($converted = $currency->convert('us dollar', 50)) { # "us dollar" found and conversion rate exists echo "<p>{$currency->symbol}{$currency->amt_f} {$currency->id} ({$currency->name}) = {$converted['symbol']}{$converted['amt_f']} {$converted['id']} ({$converted['name']})</p>"; # €50 EUR = $71.35 USD, additionaly $currency->amt_f and $converted['amt_f'] can be used for money-formatted values echo '<p>Exchange Rates Cached: '.date('r', $currency->mtime).'</p>'; # Exchange Rates Cached: Sun, 19 Jun 2011 04:35:17 +0000 } } else { echo 'Currency not found.'; }
You are using: Euro €50.00 EUR (Euro) = $72.50 USD (US Dollar) Exchange Rates Cached: Tue, 05 Jul 2011 02:14:51 +0000
All of the properties of a valid Currency object.
date_default_timezone_set('UTC'); require_once('currency.class.php'); $currency = new Currency('eur'); if(!empty($currency->id)) { echo '<pre>'.print_r($currency, TRUE).'</pre>'; } else { echo 'Currency not found.'; }
Currency Object ( [id] => EUR [name] => Euro [symbol] => € [countries] => Array # Countries that use the Euro ( [0] => eu [1] => ad [2] => at ... ) [rate] => 1 [amt] => [amt_f] => [rates] => Array # Conversion rates for currencies with Euro as base ( [aud] => 1.3521 [bgn] => 1.9558 [brl] => 2.2609 ... ) [mtime] => 1309832091 # XML/TXT file modified date (cached date) [now] => 1309832786 [xml_path] => rates.xml [txt_path] => rates.txt [iso_path] => iso.inc.php [iso] => Array (...
All of the properties of the new, converted Currency object.
date_default_timezone_set('UTC'); require_once('currency.class.php'); $currency = new Currency('eur'); if(!empty($currency->id)) { if($converted = $currency->convert('us dollar', 50)) { echo '<pre>'.print_r($converted, TRUE).'</pre>'; } } else { echo 'Currency not found.'; }
Array ( [name] => US Dollar [symbol] => $ [country] => us [id] => USD [rate] => 1.45 [countries] => Array # Countries that use the US Dollar ( [0] => us [1] => as [2] => ec [3] => fm [4] => gu [5] => io [6] => mh [7] => mp [8] => pr [9] => pw [10] => sv [11] => tc [12] => tl [13] => um [14] => vg [15] => vi ) [amt] => 72.5 [amt_f] => 72.50 # Money-formatted amount [rates] => Array ( [to] => 1.45 [from] => 0.68965517241379 ) )
For an online working example, visit the Currency Conversion page.
2 Comments
when I run the class I get these strange characters:

any ideas?
Sorted it I needed to specify the header to send UTF.
What's on your mind?
Gravatar