如何在mysql和php中使用日期选择时间戳

时间:2014-08-09 20:24:22

标签: php mysql

我想使用表中存在的不同日期来查询是否有一天的特定时间戳,到目前为止我已经提出了这段代码

$mysqli = new mysqli('localhost', 'root', 'rolemodel', 'dashboard');

$dates = "SELECT DISTINCT DATE_FORMAT(mytimestamp, '%Y-%m-%d') FROM ".$this->table_name." ORDER BY DATE(mytimestamp) ASC;";

    /* check connection */
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }

    $rowsdata = $mysqli->query($dates); // get number of rows 

    $num_rows_date  = $rowsdata->num_rows;      

   for ($i = 0; $i<=$num_rows_date; i++) {
      $current_query = "SELECT mytimestamp FROM ".$this->table_name." WHERE mytimestamp =".$rowsdata[$i]; //not sure on what to use here

}

我很困惑如何查询确切的时间戳,该时间戳应该是特定的一天以及00:00:00

示例2014-06-02 00:00:00

希望我的问题有道理。

1 个答案:

答案 0 :(得分:0)

尝试:

//$rowsdata = $mysqli->query($dates); // get number of rows 

//$num_rows_date  = $rowsdata->num_rows;      

//for ($i = 0; $i<=$num_rows_date; i++) {

//This is usually the way you want to do this with plain old arrays
if($result = $mysqli->query($dates)){
    while($row = $result->fetch_row()){
        $current_query = "SELECT mytimestamp FROM ".$this->table_name." WHERE DATE(mytimestamp) ='".$row[0] . "'";
    //do something awesome here ...
    }    
}

请参阅docs for the DATE function

但是......看起来你只是在查询从第一个位置拉出日期的同一张桌子,所以你可以保证找到那些日期的时间戳。不确定你最终会追求什么,但你可以用一个查询来完成它。