简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM