简体   繁体   English

PHP-MySQL查询-计算时间戳在特定日期(“ Ymd”)内的结果

[英]PHP - MySQL query - counting results where timestamp is within specific date (“Y-m-d”)

This is pretty basic MySQL, but I have not been able to figure this one out, how to do it correctly.. 这是非常基本的MySQL,但我无法弄清楚这一点,以及如何正确执行。

Example: I have a DB table named "table1" with a list of records of user visitors data. 示例:我有一个名为“ table1”的数据库表,其中包含用户访问者数据的记录列表。 Columns: "ID", "TM" and "IP" 栏:“ ID”,“ TM”和“ IP”

"TM" contains timestamp for when the record is stored. “ TM”包含记录存储时间的时间戳。

I have a PHP code where I loop through days from a start date to current day. 我有一个PHP代码,在其中循环从开始日期到当前日期的几天。 Like this example: 像这个例子:

// Start date
$startdateforarray = '2010-07-21';
// End date
$end_date = date("Y-m-d");

    while (strtotime($startdateforarray) <= strtotime($end_date)) {
        $timestamp = strtotime($startdateforarray);

    //Here I want to run my MySQL Query...

        $startdateforarray = date ("Y-m-d", strtotime("+1 day", strtotime($startdateforarray)));

}

Now, inside the loop I want to make a query to count how many results there are in "table1" for each day. 现在,在循环中,我想进行查询以计算每天“ table1”中有多少个结果。

So the MySQL query should be something like: 因此,MySQL查询应类似于:

"SELECT * FROM table1 WHERE TM = (day of $timestamp)" 

Of course (day of $timestamp) is where I have a problem. 当然($ timestamp的一天)是我遇到的问题。 I know that this should be pretty simple to do, but I havent found a solution yet.. 我知道这应该很简单,但是我还没有找到解决方案。

假设时间戳是指Unix时间戳,则可以

SELECT * FROM table1 WHERE FROM_UNIXTIME(TM,'%Y-%m-%d') =  '2010-07-21'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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