找出数据库中两列之间的两个日期之间的天数差异,并将该差异放入变量以供在php中使用

时间:2016-05-27 13:12:51

标签: php mysql

我认为mysql可以计算它,所以我创建了一个名为$sqlDateDiff的变量。

$servername = "localhost";$username = "root";$password = "";$dbname = "maplibrary";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
$sqlDateDiff = "SELECT DATEDIFF(BorrowedDate, ReturnedDate) 
                FROM bookstakenout WHERE BorrowNumberID = $bnid" ;

$result = mysqli_query($conn, $sqlDateDiff);

然后我想使用$result创建一个嵌套的if语句。如何将差异存储在变量中以在if语句中使用。

bookstakenout表中,ReturnedDate/BorrowedDate都是yyyy-mm-dd

  

块引用

1 个答案:

答案 0 :(得分:0)

查询:DATEDIFF(日期部分,开始日期,结束日期)为DIFFDATE