php - 如何在 PHP 中从 SQL 数据库获取最近的日期匹配

标签 php mysql sql

我目前正在尝试根据 SQL 数据库获取最接近的 2 组日期 (d-m-Y) 匹配,然后输出匹配的 InterestRate 列名称。

我有一个当前代码的示例,这主要是伪代码并尝试了一些东西。如果有帮助的话。

//$XSS_BLOCK2 = "05-05-2016";
$XSS_BLOCK3 = "20-05-2016"; //By the way The '2016-05-20' is user input so it will not be '2016-05-20' all the time, so it could be anything '2014-08-15'.
$today = date('d-m-Y');
$interest = 0;
$securesqlstring = $secureconn->prepare("SELECT * FROM LatePaymentRates");
$securesqlstring->execute();
while($row=$securesqlstring->fetch())
  {
     echo $row['StartDate'];
     echo $row['EndDate'];
     echo $row['InterestRate'];

    $varsin = array($XSS_BLOCK3, $today);
    $DateRange = new DateTime($varsin);
    $databasein = array($row['StartDate'], $row['EndDate']);
    $DateRanges = new DateTime($databasein);
    if(($DateRange >= $DateRanges) && ($DateRange >= $DateRanges)) {

      $dayrate = $row['InterestRate'] * $XSS_BLOCK3 / 36500;


    $start_date = new DateTime($DateRange);
    $end_date = new DateTime($DateRanges);
    $dd = date_diff($end_date, $start_date) * $dayrate;
    $interest += $dayrate;
    }

  }
  $LatePaymentInterest = $interest;
if (!$securesqlstring) // If there is an error it will show this message.
  {exit("Error in the SQL");}

最佳答案

我们真的需要把这个拼出来吗?我觉得我一定错过了一些东西......

SELECT * FROM my_table WHERE '2016-05-20' BETWEEN startdate AND enddate;

关于php - 如何在 PHP 中从 SQL 数据库获取最近的日期匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37226254/

相关文章:

PHP上传到数据库

c# - 使用 ClosedXML 库导出 GridView

java.sql.SQLException : Exhausted Resultset error 异常

sql - Postgresql 枢轴?交叉表?

javascript - document.getElementById ('' ).style.display ='block' ;不在 Wordpress 中工作

php - MYSQL/PHP 在保持数字顺序的同时向上或向下移动对象的顺序

mysql - 从多行 MYSQL 中获取 1 行

php - 我如何在 CakePHP 中获取 Controller 名称列表以验证用户数据?

php - 如何在现有的默认身份验证上使用 Laravel 创建自定义身份验证

sql - 你怎么称呼你的美国国家表?