www.webdeveloper.com
Results 1 to 6 of 6

Thread: Automatic conversion of a Euro value to Pounds

  1. #1
    Join Date
    May 2014
    Posts
    1

    Question Automatic conversion of a Euro value to Pounds

    Creating a web page I want to inform visitors the price of an item that is available elsewhere. I could say just 125.87 but I would rather have an underlining function that automatically converted it to pounds depending on the exchange rate of the day. Is this a bridge too far or is it indeed possible? If so how?

    David

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271
    The European Central Bank publish many things about Foreign exchange references rates...

    At first this page http://www.ecb.europa.eu/euro.html give the daily reference rates which are usually updated by 3 p.m. C.E.T. They are based on a regular daily concertation procedure between central banks across Europe and worldwide, which normally takes place at 2.15 p.m. CET.

    Second a special page for developers propose many PHP code to parse the data

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271
    Here is a newly created Euro universal converisseur. I will still put in a few points to make...
    Last edited by 007Julien; 05-10-2014 at 08:23 PM.

  4. #4
    Join Date
    Nov 2010
    Posts
    1,097
    you can use yql to get the current exchange rate, then just multiply as you need to...

    Code:
    <script>
     
    
      function parseExchangeRate(data) {
        var name = data.query.results.row.name;
        var rate = parseFloat(data.query.results.row.rate, 10);
        alert("Exchange rate " + name + " is " + rate);
      }
      
        var script = document.createElement('script');
        script.setAttribute('src', "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3DEURGBP%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate");
        document.body.appendChild(script);
      
    </script>

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271
    Try to upload this php page on your server
    PHP Code:
    <?php

        header
    ("expires: mon, 26 jul 1997 05:00:00 gmt");
        
    header("cache-control: no-cache, must-revalidate");
        
    header("pragma: no-cache");
        
    header("Content-Type:text/html;charset=utf-8");

    /*    The reference rates are usually updated by 3 p.m. C.E.T.
        They are based on a regular daily concertation procedure between central banks
        across Europe and worldwide, which normally takes place at 2.15 p.m. CET.*/

        
    date_default_timezone_set('Europe/Paris');
        
    $fileName='ECB_GB_pound_rate.txt';
        
    $okFle=false;
        if (
    file_exists($fileName) && ($tms=filemtime($fileName))) {
            
    $dteLstMdf=new DateTime(date ("Y-m-d H:i:s.",$tms));
    //        echo "Date of the last file : ".$dteLstMdf->format('Y-m-d H:i:s')."<br>";
            
    $dtePub=clone($dteLstMdf);
            
    $dtePub->setTime(15,15);
            if (
    $dtePub<$dteLstMdf$dtePub->modify("-1 day");
    //        echo "First possible pubDate after modif : ".$dtePub->format('Y-m-d H:i:s')."<br>";
            
    while(5<$dtePub->format('N')) $dtePub->modify("+1 day");
    //        echo "First possible bussness day after modif : ".$dtePub->format('Y-m-d H:i:s')."<br>";
            
    $okFle=new DateTime()<$dtePub;
        }

        if (
    $okFle) { $gb_pound_rate=trim(file_get_contents(ECB_GB_pound_rate.txt));}
        else {
    // Lecture des taux
            
    $HTMLines=file("http://www.ecb.europa.eu/euro.html");
            
    $strDte='var ECB_date="';
            
    $strRte='var ECB_GBP_rate="';
            foreach (
    $HTMLines as $v) {$w=trim($v);
                if (
    preg_match("/>Date: ([^<]+)</",$w,$m)) $strDte.=$m[1].'";'.chr(13).chr(10);
                  if (
    preg_match("/([\w ]+)\(GBP\)\s?=\s?([\d.]+)/",$w,$m)) $strRte.=$m[2].'";'.chr(13).chr(10);
            }
            
    $gb_pound_rate="$strDte$strRte";
            
    file_put_contents($fileName,"$strDte$strRte");
         }
    ?>
    <!doctype html>
    <html lang="fr">
    <head>
      <meta charset="utf-8">
      <title>Euro pound converter</title>
    <style type="text/css">
    body {font-family:Georgia,Serif;background:#000;color:#fff}
    #pge {width:600px;margin:30px auto;padding:13px;background:#888;}
    </style>
    </head>
    <body>
    <div id="pge"><fieldset><legend>Consersion Euro pounds</legend>
        <p id="rsp"></p>
    </fieldset></div>
    <script>
    <?php echo $gb_pound_rate?>
    // Your script
        document.getElementById('rsp').innerHTML='At  '+ECB_date+' according to ECB :<br> 1,000 Euros are '+(ECB_GBP_rate*1000).toFixed(2)+' Pounds, ';
        document.getElementById('rsp').innerHTML+='then 1,000 Pounds are '+(1000/ECB_GBP_rate).toFixed(2).replace(/(\d)(?=(\d{3})+\b)/g,'$1,')+' Euros.';
    </script>
    </body>
    </html>
    This page store the rate in a file and use this file without new possible publications from ECB. See the resulting script on this page which allow all calculation with the rate.
    Last edited by 007Julien; 05-12-2014 at 05:34 PM. Reason: complements

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles