条件条件为日期时的mysql查询

时间:2018-07-01 05:19:50

标签: php mysqli

我正在对mysql进行查询,条件是从今天起4天之后所有具有tripdate的行。 但是我的脚本无法正常工作,在我的数据表日期格式中,日期格式为DD / MM / YYYY,列类型为“ varchar”。

当我执行这些查询时,我得到所有日期的行,条件不起作用。

这是sql脚本

    <?php


include('../../connect.php');

$date = strtotime("+4 day", time());
$finaldate = date('d/m/Y', $date);
echo $finaldate;


//$sql = "SELECT * FROM tblapps WHERE pay_status <> 'paid' AND appstatus <> 'Not Interested' AND process = 'yes' AND journeydate >= '$finaldate'";
$sql = "SELECT * FROM tblapps WHERE journeydate >= '$finaldate'";
if($result = mysqli_query($connect, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>id</th>";
                echo "<th>Pay Status</th>";
                echo "<th>Journey date</th>";
                echo "<th>email</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['pay_status'] . "</td>";
                echo "<td>" . $row['journeydate'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // Free result set
        $rowcount=mysqli_num_rows($result);
        echo $rowcount;

        mysqli_free_result($result);        


    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

1 个答案:

答案 0 :(得分:1)

不要将日期存储为文本,将其存储为日期。对于临时的解决方法,您可以使用STR_TO_DATE

SELECT *
FROM tblapps
WHERE STR_TO_DATE(journeydate, '%d/%m/%Y') >= '2018-06-01';