Select data between two dates?

后端 未结 5 416
灰色年华
灰色年华 2020-12-06 08:46

I\'m using a database to store logs, with a column \"date\" which holds the date it was inserted. The format of the date is \"MM/DD/YY\". Please can anyone suggest how I wou

5条回答
  •  天涯浪人
    2020-12-06 08:55

    Change date parameters into Unix timestamps and then compare them. Here is the code:

    $from_date = "2019/01/12";
    $to_date = "2019/01/15";
    
    $from_date_unix = strtotime($from_date);
    $to_date_unix = strtotime($to_date);    
    
    $result = mysql_query("SELECT * FROM logs WHERE date >= " . $from_date_unix . " AND date <= " . $to_date_unix . " ORDER by id DESC");
    while($row = mysql_fetch_array($result)) {
    // display results here
    }
    

提交回复
热议问题