Currency Conversion in CodeIgniter

Currency conversion for websites is one of those things where there’s lots of solutions, but all of them cost money.  I had to do this some time ago for an international procurement tool and, like most people, we had to use a paid solution.  But it got me thinking – currency data is just a series of numbers and must be published by someone, somewhere…

Then, one day, I found an XML feed from the European Central Bank that gave current and historical currency data.  This is updated daily (14:00 GMT) and it corresponds to all the other currency data out there.  It occured to me that, unless you run an ecommerce site that banks in *EVERY* currency there is, most of the time you will be banking in one currency and simply displaying another.

So, if you’re a British shop and a user displays the money in Euros, they will actually be charged the figure in GBP which their bank will convert into EUR to take it out of their account.  So just displaying the figure in the local currency is a convenience, but won’t change how the site operates.

I’ve written this model in what is, I hope, is a logical way.  That is to say that there are two completely separate processes:

  1. The data is imported from the XML feed, usually by a cronjob
  2. You query that data from the database

This avoids a user having a lag whilst the XML is retrieved and parsed.  Typically, this is 2-4 seconds.

Conversion Instructions

This is what most of you want, how to convert one figure into another.  In your controller, simply do this:

<?php

class some_controller extends CI_Controller {

    public function some_controller() {
        /* Load the model */
        $this->load->model('currency');

        /* Convert */
        $arrCurrency = $this->currency->convert('GBP', 'USD', '9843.25');

    }

}

?>

This will convert 9,843.25 British Pounds into US Dollars.  This returns an array of data that is useful in later programming:

Array
(
    [from] => GBP /* The from currency */
    [to] => USD /* The to currency */
    [date] => 2012-01-13 /* The date the conversion is from - might not be today */
    [rate] => 1.5327652424388 /* The conversion rate between GBP and USD */
    [reverse_rate] => 0.65241562915982 /* The conversion rate between USD and GBP */
    [from_amount] => 9843.25 /* The amount in GBP */
    [to_amount] => 15087.39 /* The amount in USD */
)

Getting the Symbol

Getting the currency as a string is very important for output.  The fetch_symbol() method gets the correct output of symbol and numbers.

<?php

class some_controller extends CI_Controller {

    public function index() {
        $this->currency->fetch_symbol('GBP', '1000');
        /* British Pounds - outputs '£1,000.00' */

        $this->currency->fetch_symbol('DKK', '803');
        /* Danish Kroner - outputs 'kr 803.00' */

        $this->currency->fetch_symbol('CHF', '27.3');
        /* Swiss Francs - outputs '27.30 ₣' */

        $this->currency->fetch_symbol('CZK', '10354.43');
        /* Czech Koruna - outputs '10,354.43' no symbol */

    }

}

?>

Importing the Data

When you first load the model, by default it will create the data table and import the data from the ECB.  However, this should be done daily to keep the data up-to-date.  The ECB releases the data every day at 13:00 Frankfurt time (14:00 UTC) but, it may be an idea to actually do the data update overnight during your site’s down time.  Creating the cronjob is very simple in CodeIgniter v2:

<?php

class cron extends CI_Controller {

    public function update_currency() {

        /* Load the model */
        $this->load->model('currency');

        /* Import the data */
        $this->currency->import();

    }

}

?>

All you need to do now is set the cronjob up on your server.  In this example, I’ve set it so that it checks it Monday – Friday at 14:05 and 14:35 (in case they’re late).  This server’s timezone is London.

# Fetch Currency Rates - ECB releases data about 14:00, Mon-Fri
5,35 14 * * 1-5 /usr/bin/php5 /path/to/index.php cron update_currency >/dev/null 2>&1

And that’s it.  You now have accurate currency conversion on your site.

Download

Currency.zip

Leave a Reply