2016-05-14 36 views
0

我目前正試圖針對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");} 
+1

你在找什麼問題或錯誤? – Edu

+1

定義'最接近的匹配' – Strawberry

+1

另外,請記住您的數據庫有時間。這會影響範圍搜索,因爲只有日期的日期時間僅等於其日期和00:00:00。例如:14-05-2016!= 14-05-2016 01:02:03 – Edu

回答

1

我們真的需要拼出這個嗎?我覺得我必須錯過一些東西......

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

順便說一句'2016-05-20'是用戶輸入,所以它不會總是'2016-05-20',所以它可能是任何'2014-08-15'。 – Mattlinux1

+0

@ Mattlinux1很好看。 – Strawberry

+0

此外,查詢需要考慮日期之間有一些移動的月份,因爲您可以告訴我目前不熟悉SQL。 – Mattlinux1

相關問題