Currency generator - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Currency generator.php

  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  3.  
  4.         error_reporting (E_ALL & ~E_NOTICE);
  5.  
  6.         $API_URL = 'https://api.exchangeratesapi.io/'; //url
  7.         $START_DATE = isset($_POST['start']) && strlen($_POST['start'])==10 ? $_POST['start'] : '2020-01-01';
  8.         $END_DATE = isset($_POST['end']) && strlen($_POST['end'])==10 ? $_POST['end'] : '2020-12-31';
  9.         $CURRENCY = isset($_POST['currency']) && strlen($_POST['currency'])==3 ? $_POST['currency'] : 'USD';
  10.         $BASE = isset($_POST['base']) && strlen($_POST['base'])==3 ? $_POST['base'] : 'EUR';
  11.         $txt = '';
  12.  
  13.  
  14.         $url = "${API_URL}history?start_at=${START_DATE}&end_at=${END_DATE}&base=${BASE}&symbols=${CURRENCY}";
  15.  
  16.         $rates = json_decode(file_get_contents($url), true)['rates'];
  17.         $daterange = new DatePeriod(new DateTime($START_DATE), new DateInterval('P1D'), new DateTime($END_DATE." 23:59"));
  18.  
  19.         foreach($daterange as $date){
  20.                 $date_key = $date->format("Y-m-d");
  21.                 $date_string = $date->format("Y;m;d");
  22.  
  23.           if (isset($rates[$date_key])){
  24.               $previous = $rates[$date_key][$CURRENCY];
  25.           }
  26.  
  27.           if (!isset($previous)){
  28.               $previous = json_decode(file_get_contents("${API_URL}${date_key}?base=${BASE}&symbols=${CURRENCY}"), true)['rates'][$CURRENCY];
  29.           }
  30.  
  31.           $txt .= "${date_string};". $previous*10000 ."\n";
  32.         }
  33.  
  34.         header("Content-type: text/csv");
  35.         header("Content-Disposition: attachment; filename=rates.csv");
  36.         header("Pragma: no-cache");
  37.         header("Expires: 0");
  38.         echo($txt);
  39.         die;
  40. }
  41.  
  42. ?>
  43.  
  44. <!DOCTYPE html>
  45. <html lang="en">
  46. <head>
  47.         <meta charset="UTF-8">
  48.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  49.         <title>Currency generator</title>
  50.         <style>
  51.                 div{
  52.                         margin: 1rem auto;
  53.                 }
  54.  
  55.                 input,
  56.                 button{
  57.                         border: 1px solid blue;
  58.                 }
  59.         </style>
  60. </head>
  61. <body>
  62.         <h1>Currency rates CSV generator</h1>
  63.         <span>based on <a href="https://exchangeratesapi.io/" target="_blank">https://exchangeratesapi.io/</a></span>
  64.         <form action="" method="POST">
  65.                 <div>
  66.                         Start date: <input type="date" name="start" value="<?php echo date('Y-01-01'); ?>">
  67.                 </div>
  68.                 <div>
  69.                         End date: <input type="date" name="end" value="<?php echo date('Y-m-d'); ?>">
  70.                 </div>
  71.                 <div>
  72.                         Currency: <input type="text" name="currency" maxlength="3" minlength="3" value="USD">
  73.                 </div>
  74.                 <div>
  75.                         Base currency: <input type="text" name="base"  maxlength="3" minlength="3" value="EUR">
  76.                 </div>
  77.                 <button type="submit">Generate</button>
  78.         </form>
  79.        
  80. </body>
  81. </html>
File Description
  • Currency generator
  • PHP Code
  • 25 Jan-2021
  • 2.4 Kb
You can Share it: