我目前正试图针对SQL数据库获取2组日期(d-m-Y)的最接近匹配,然后将匹配的InterestRate列名称输出。如何从SQL数据库中获取PHP中最近的日期匹配
我有我目前的代码的例子,这主要是伪代码,并尝试了几件事。如果有帮助。
//$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");}
你在找什么问题或错误? – Edu
定义'最接近的匹配' – Strawberry
另外,请记住您的数据库有时间。这会影响范围搜索,因为只有日期的日期时间仅等于其日期和00:00:00。例如:14-05-2016!= 14-05-2016 01:02:03 – Edu