如何从SQL数据库获取PHP中最接近的日期匹配 [英] How to get the nearest date match in PHP from a SQL Database

查看:101
本文介绍了如何从SQL数据库获取PHP中最接近的日期匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试针对SQL数据库获取2组日期(d-m-Y)的最接近匹配项,然后将匹配的InterestRate列名放出.

I am currently trying to get the nearest match of 2 sets of dates (d-m-Y) against a SQL database and then out put the matched InterestRate column name.

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

I have an example of my current code, this is mostly pseudo code and trying a few things out. If that helps.

//$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");}

推荐答案

我们真的需要把这个拼出来吗?我觉得我一定很想念东西...

Do we really need to spell this one out? I feel like I must be missing something...

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

这篇关于如何从SQL数据库获取PHP中最接近的日期匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆