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

标签 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

希望我的问题有意义。

最佳答案

尝试:

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

但是...看起来您只是在查询最初从中提取日期的同一个表,因此您一定能找到这些日期的时间戳。不确定您最终想要什么,但您也许可以通过单个查询来完成。

关于php - 如何在mysql和php中使用日期选择时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25222696/

相关文章:

php - PHP/HTML替换/模板引擎

mysql - MySQL 中有没有一种方法可以计算一年中也已在前几年注册的成员数量?

MySQL 如何从同一列中选择多个值,以便它们出现在不同的列中?

php - f3框架sql查询连接

javascript - 我想更新当前页面 codeigniter 中的 ID

javascript - JavaScript 接收请求失败错误

php - 如何使用 MYSQLi 查询从数据库中以 php 显示图像?

javascript - CSS 和 JavaScript 都没有使用 codeigniter 在移动 View 中加载?

php - 使用 PHP 和 MySQL 生成分页链接时,如何传递用户通过查询字符串输入的多个搜索参数?

mysql - 在 SQL 中从另一个表中选择一列和 SUM() 值