1

I have database with a list of currencies and their rates, also i have the history for them as well, the base rate is usd,

now the question is my user select a different base rate ie AED, EGP, BRL

I need to convert all my values dynamically on that base rate user has selected.

Q: what is the formula for rate conversion and is there a php or mysql way of doing this.

I have search high and low for this and i keep getting stomped

user2115506
  • 23
  • 1
  • 6
  • there is no php or mysql way, this is a simple math problem. Do you have the rate of conversion for each currency? I'll suggest you save all your values in just one currency (US dollars), and as you display in another currency, you multiply by the rate – Ibu Oct 31 '13 at 18:15
  • Please give us some example rows from your database table – cmorrissey Oct 31 '13 at 18:16
  • `$amount*$usd_rate` maybe? – geomagas Oct 31 '13 at 18:17
  • have the rates but I do not know how to to the conversion, i read some articles but it seems to not out put right – user2115506 Oct 31 '13 at 18:17
  • base is now EGP -Egyptian pound -6.8932, so i need to convert all the currencies to egp – user2115506 Oct 31 '13 at 18:18

1 Answers1

0

An extremely simple way of doing this would be to use a freely available API such as appspot's rateexchange: http://rate-exchange.appspot.com/currency?from=USD&to=EGP&q=10

Where from= the source currency, to= the target currency, and q= the amount of source currency.

You could then easily use PHP's cURL to GET the resulting JSON, and use json_decode to convert it into usable PHP.

Example:

function get($url)
{ 
    $ch = curl_init ($url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    return curl_exec ($ch);
}

$json = get("http://rate-exchange.appspot.com/currency?from=USD&to=EUR&q=10");
$array = json_decode($json, true);

print_r($array);
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • Thanks, I will give it some thought on how to implement this, but the problem is that i have over 50 currencies to change from and there is also the history so if the wanted to see 1 months, how am i going to do that – user2115506 Oct 31 '13 at 18:34
  • That's a significantly larger undertaking! I'd recommend going directly to Google's currency API https://code.google.com/p/currency-converter-api/ then -- much more challenging to implement, but will have all of the historical features and the ability to return multiple results. You may find yourself needing to cache some data in a local database? Here is an example of a more sophisticated site built on their API: http://currencies.apps.grandtrunk.net/ – brandonscript Oct 31 '13 at 18:46
  • Another option, if you're not keen on using Google, Yahoo offers a similar finance API and actually provides a php library: https://code.google.com/p/yahoo-finance-managed/wiki/MainPage?tm=6 – brandonscript Oct 31 '13 at 18:49
  • thank you all for the the suggestions, I will have a look at the yahoo and google – user2115506 Oct 31 '13 at 19:02
  • Just be aware that google and yahoo aren't free for commercial use. The formula to convert is very simple. By definition you are always storing rates against a base currency. So lets say you have Currency1, Currency2 & CurrencyBase. You will have stored the rate of Currency1/CurrencyBase as Cur1Rate and the rate Currency2/CurrencyBase as Cur2Rate. To get Currency1/Currency2 rate Cur1Rate/Cur2Rate. You can do this dynamically as required. I do it on my site here http://currency.creative-dreaming.com/Home/Resources (disclaimer this is my own commercial site). – Dale K Dec 13 '13 at 00:48