10 months ago on July 14
Download free source files for this article. Example

Currencies

Images_of_Money

Note: Country and currency codes are added and removed periodically by the ISO and not reflected in this example. For that, please see Download Free ISO Codes and Exchange Rates.

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.

The Conversion Process

  • Check for valid currency against iso.inc.php (By name, code, country name, or country code)
  • If to be converted, check that a rate exists for both currencies
  • Return conversion

Available Rates

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:

  • AUD - Australian Dollar
  • BGN - Bulgarian Lev
  • BRL - Brazilian Real
  • CAD - Canadian Dollar
  • CHF - Swiss Franc
  • CNY - Yuan Renminbi
  • CZK - Czech Koruna
  • DKK - Danish Krone
  • EUR - Euro
  • GBP - Pound Sterling
  • HKD - Hong Kong Dollar
  • HRK - Croatian Kuna
  • HUF - Forint
  • IDR - Rupiah
  • ILS - New Israeli Sheqel
  • INR - Indian Rupee
  • JPY - Yen
  • KRW - Won
  • LTL - Lithuanian Litas
  • LVL - Latvian Lats
  • MXN - Mexican Peso
  • MYR - Malaysian Ringgit
  • NOK - Norwegian Krone
  • NZD - New Zealand Dollar
  • PHP - Philippine Peso
  • PLN - Zloty
  • RON - New Leu
  • RUB - Russian Ruble
  • SEK - Swedish Krona
  • SGD - Singapore Dollar
  • THB - Baht
  • TRY - Turkish Lira
  • USD - US Dollar
  • ZAR - Rand

Examples

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.

<?php
 
/*
# ! 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.';
}
 
?>
Output
You are using: Euro

€50.00 EUR (Euro) = $72.50 USD (US Dollar)

Exchange Rates Cached: Tue, 05 Jul 2011 02:14:51 +0000

Currency Object Properties

All of the properties of a valid Currency object.

<?php
 
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.';
}
 
?>
Output
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
		(...

Converting Currency

All of the properties of the new, converted Currency object.

<?php
 
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.';
}
 
?>
Output
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.

Short URL: http://mayavps.com/a15
If you enjoyed this post, please subscribe via e-mail, RSS, or Twitter.

2 Comments

David

United Kingdom Windows Firefox

when I run the class I get these strange characters:



any ideas?

0

David

United Kingdom Windows Firefox

Sorted it I needed to specify the header to send UTF.

0

What's on your mind?

Gravatar

HTML not converted. Links begin with http://. Code blocks should be wrapped with [code][/code]. Unicode characters accepted.


E-Mail RSS