現在、SQLデータベースに対して2組の日付(d-m-Y)のうち最も近いものを取得し、一致するInterestRate列名を出力しようとしています。PHPでSQLデータベースから最も近い日付を取得する方法
私の現在のコードの例があります。これは主に擬似コードであり、いくつかのことを試しています。それが助けば。
//$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