...

View Full Version : currency conversion



bunny1
03-21-2007, 02:38 PM
Is there a way of checking and displaying the currency conversion for price using php?
I want to connect to the database get the price in one currency and then check the rate and display the second curreny.

thanks

maghiel
03-21-2007, 04:06 PM
http://nl2.php.net/manual/nl/function.number-format.php

think you have to create the rate database yourself

bunny1
03-21-2007, 04:27 PM
oh great!
thanks

Len Whistler
03-21-2007, 09:06 PM
You can use PHP to directly access currency exchange rates from Yahoo Finance in real time. The code below will convert $100 US Dollars into Canadian dollars and since currencies are traded almost 24/7 the output always changes. Demo of the script: http://www.rgbpixels.ca/dollar.php

You would of course have to change the $ticker variable and make some other changes for your needs. Go to Yahoo Finance for the currency symbols.


<?php
$us_price = '100';
$ticker= 'CADUSD=X';
$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$ticker&f=sl1d1t1c1ohgv&e=.csv", "r");
$exchange_rate = fread($open, 2000);
fclose($open);
$exchange_rate = str_replace("\"", "", $exchange_rate);
$exchange_rate = explode(",", $exchange_rate);
$ca_price = ($us_price/$exchange_rate[1]);
$price = number_format ($ca_price, 2);

echo "$us_price US dollars = \$$price Canadian dollars";
?>

bunny1
03-22-2007, 01:07 AM
thank you very much!

bunny1
03-22-2007, 03:41 AM
any idea why this might be very slow loading on a page.
and how i would speed it up?
is it to do with the server?

tx

Len Whistler
03-22-2007, 04:20 AM
any idea why this might be very slow loading on a page.
and how i would speed it up?
is it to do with the server?

tx

It could be your host that is slow. The script does have to query Yahoo first so there will be a small delay.

ArcticFox
03-22-2007, 12:34 PM
Is there a way to do this for multiple prices without having to insert all that code into each section?

To look like:

Price: $50 ($550.66 pesos)
Price: $74 ($814.98 pesos)
Price: $99.95 ($1,100.77 pesos)



Maybe something like


Price: <?php echo "$50 " + "(" + (50 * $mxn_conv) + " pesos)" ?>
Price: <?php echo "$74 " + "(" + (74 * $mxn_conv) + " pesos)" ?>
Price: <?php echo "$99.95 " + "(" + (50 * $mxn_conv) + " pesos)" ?>


{edit} Nightfire's got it: http://www.codingforums.com/showthread.php?p=548501&posted=1 {/edit}


And, is there a way to keep an error from showing if the data happens to be missing? (change "CADUSD=X" to "000000=X" to test)

Len Whistler
03-22-2007, 08:52 PM
And, is there a way to keep an error from showing if the data happens to be missing? (change "CADUSD=X" to "000000=X" to test)

You are right, data will be missing if Yahoo is too slow and an error will come up, but it doesn't happen often.

One way around that is to have the script load the information into a local database (MySQL, text file?) on a regular basis using a CRON job or PHP and then your site would get the currency quotes from your database instead of Yahoo. I don't know anything about CRON jobs, but I assume it can handle something like this.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum