当前位置: 动力学知识库 > 问答 > 编程问答 >

How to select a timestamp using date in mysql and php

问题描述:

I want to query if there is a particular timestamp for a day using the distinct dates that are present in a table and so far I have come up with this code

$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

}

I am confused on how to query the exact timestamp which is supposed to be the particular day along with the 00:00:00

Example 2014-06-02 00:00:00

Hope my question makes sense.

网友答案:

try:

//$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 ...
    }    
}

See the docs for the DATE function.

BUT ... it looks like you're just querying the same table you pulled the dates from in the first place, so you're guaranteed to find timestamps with those dates. Not sure what you're ultimately after, but you may be able to do it with a single query.

分享给朋友:
您可能感兴趣的文章:
随机阅读: