|
Posted by Treefrog on 11/18/63 11:43
meenasamy@hotmail.com wrote:
> Hi all, i need to create a function that takes three parameters(
> Original currency, needed currency, amount) i need to convert from the
> original currency to the needed currency the amount and return the new
> amount in the needed currency, I need this function to work real time
> (obtaining real time currency exchange rates), any ideas???
Here's one a wrote earlier.
You need to buy (or scrape ;o) data from www.oanda.com or xe or
wherever, the program I wrote stores it in a database. I needed
historical data as well as current exchange rates, hence the date
field. $source and $dest are the three letter currency code e.g. "GBP"
or "USD".
The function getRate returns the exchange rate (from the db) in
relation to GBP for the given date... although, any "intermediate"
currency will work, I chose GBP because that's my local flavour.
function exchange($source,$dest,$amount,$date=null)
{
// if there's no date, then presume today.
if ($date==null)
{
$date = date("Ymd");
}
// GET SOURCE DATA
$sourceRate = getRate($source,$date);
// GET DEST DATA.
$destRate = getRate($dest,$date);
// convert "amount" from sourceRate to GBP
if ($sourceRate > 0 && $destRate > 0)
{
$sourceGBP = $amount * $sourceRate;
$destAmount = $sourceGBP / $destRate;
return round($destAmount,2);
}
else
{
return false;
}
} // end function exchange
Navigation:
[Reply to this message]
|