PHP Currency Conversion

August 24th, 2012

This PHP Currency Conversion script converts a value from one currency to a selection of currencies which you define. Rates are parsed from the European Central Bank’s XML Feed, which is updated daily. I’ve added symbol support and a currency names for the 33 currencies so you don’t need to look up any of the currency codes. Download links are at the bottom of the page.

This screenshot below shows the Currency Conversion script at work.

PHP Currency Conversion Script

The PHP Currency Conversion script can work in two ways (see DEMOS below) :

PHP Currency Conversion requirements

The requirements to run the script are:

  • A host which supports PHP and MySQL
  • PHP 5+ (The Class uses SimpleXML functions)
  • PHP’s CURL Extension

Nearly all hosts have the CURL extension compiled. To check the PHP version and whether the CURL extension is loaded, run phpinfo().

Install PHP Currency Conversion code

  • Create a database to hold the currency table and add a user.
  • Change $tablename from the default name “currency” (if required) in classes/class_currency_convertor.php
  • Open your favourite text editor and open php/connect.php“.
  • Edit $host, $user, $password and $db variables to match your system

Run the update_rates() function by running the currency_update.php script.
This will create the table to hold the currency information (if it doesn’t already exist), and get the latest rates from the European Central Bank. I recommend that you set this as a CRON job so that it runs periodically.

PHP Currency Conversion Functions

<?

// Single currency conversion from 2140 GBP to USD 
print $currency->convert(2140, 'GBP', 'USD');

// Select box with multiple values
print $currency->convert_multiple(2140, 'GBP', 'AUD|ZAR|RON');

?>

The syntax:
Single currency conversion: (cost, original currency code, destination currency code)
Multiple currency conversion:(cost, original currency code, destination currency codes separated with a “|”)

The Multiple currency conversion uses AJAX to populate a element with the id “cost”.
A request is sent to the file ajax_currency.php and the #cost is updated with the result.

The PHP Currency Conversion script is free to use. Please leave a comment below.

Download PHP Currency Conversion script

Click here to download PHP Currency Conversion script