Currency generator - PHP Online
Form of PHP Sandbox
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
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.
Result of php executing
Full code of Currency generator.php
- <?php
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- error_reporting (E_ALL & ~E_NOTICE);
- $API_URL = 'https://api.exchangeratesapi.io/'; //url
- $START_DATE = isset($_POST['start']) && strlen($_POST['start'])==10 ? $_POST['start'] : '2020-01-01';
- $END_DATE = isset($_POST['end']) && strlen($_POST['end'])==10 ? $_POST['end'] : '2020-12-31';
- $CURRENCY = isset($_POST['currency']) && strlen($_POST['currency'])==3 ? $_POST['currency'] : 'USD';
- $BASE = isset($_POST['base']) && strlen($_POST['base'])==3 ? $_POST['base'] : 'EUR';
- $txt = '';
- $url = "${API_URL}history?start_at=${START_DATE}&end_at=${END_DATE}&base=${BASE}&symbols=${CURRENCY}";
- $rates = json_decode(file_get_contents($url), true)['rates'];
- $daterange = new DatePeriod(new DateTime($START_DATE), new DateInterval('P1D'), new DateTime($END_DATE." 23:59"));
- foreach($daterange as $date){
- $date_key = $date->format("Y-m-d");
- $date_string = $date->format("Y;m;d");
- if (isset($rates[$date_key])){
- $previous = $rates[$date_key][$CURRENCY];
- }
- if (!isset($previous)){
- $previous = json_decode(file_get_contents("${API_URL}${date_key}?base=${BASE}&symbols=${CURRENCY}"), true)['rates'][$CURRENCY];
- }
- $txt .= "${date_string};". $previous*10000 ."\n";
- }
- header("Content-type: text/csv");
- header("Content-Disposition: attachment; filename=rates.csv");
- header("Pragma: no-cache");
- header("Expires: 0");
- echo($txt);
- die;
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Currency generator</title>
- <style>
- div{
- margin: 1rem auto;
- }
- input,
- button{
- border: 1px solid blue;
- }
- </style>
- </head>
- <body>
- <h1>Currency rates CSV generator</h1>
- <span>based on <a href="https://exchangeratesapi.io/" target="_blank">https://exchangeratesapi.io/</a></span>
- <form action="" method="POST">
- <div>
- Start date: <input type="date" name="start" value="<?php echo date('Y-01-01'); ?>">
- </div>
- <div>
- End date: <input type="date" name="end" value="<?php echo date('Y-m-d'); ?>">
- </div>
- <div>
- Currency: <input type="text" name="currency" maxlength="3" minlength="3" value="USD">
- </div>
- <div>
- Base currency: <input type="text" name="base" maxlength="3" minlength="3" value="EUR">
- </div>
- <button type="submit">Generate</button>
- </form>
- </body>
- </html>
File Description
- Currency generator
- PHP Code
- 25 Jan-2021
- 2.4 Kb
You can Share it:
Latest PHP Pastes