简体   繁体   English

PHP Mysql获取日期范围的统计信息

[英]PHP Mysql get stats for a date range

I am getting a count of scans in a database. 我正在数据库中进行扫描计数。 The time field is a mysql timestamp (2011-10-20 14:15:12). 时间字段是mysql时间戳(2011-10-20 14:15:12)。 I have a function that lets me set a timeframe like 30 days, 60 days etc... this was working for weeks. 我有一个功能,可让我设置30天,60天等时间范围,这已经工作了几周了。 Then I just noticed it broke. 然后我才发现它坏了。

function getScans($timeframe = 0)
{
    if ($timeframe != 0) { 
        $query = 'SELECT COUNT( * ) 
            FROM stats 
            WHERE time <= curdate( )+1 
            AND time >= curdate( )-' . ($timeframe - 1);
    } else {
        $query = 'SELECT COUNT( * ) 
            FROM stats';
    }

    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    return $row[0];
}

I know you have all heard this before but this was completely working last week. 我知道你们以前都听说过,但这在上周已完全起作用。 I went back today and noticed that when the $timeframe is other than 0, it no longer works. 我今天回去,发现当$ timeframe不是0时,它不再起作用。 Any help is appreciated. 任何帮助表示赞赏。

The problem is this: curdate( )+1 yields 20111131 (to day 2011-11-30) not what I believe you expect, 2011-12-01 , the same is true for the later curdate() call. 问题是这样的: curdate( )+1产生20111131 (到20111131日),而不是我所希望的2011-12-01 ,以后的curdate()调用也是如此。 It's probably been working fine because earlier in the month the calls have resulted in correct dates and MySQL accepts the formatting, but now it fails to alert on "impossible" dates. 它可能运行良好,因为在当月早些时候调用产生了正确的日期,并且MySQL接受了格式设置,但是现在它无法警告“不可能”的日期。 Closing in on next month, things start acting up. 下个月关闭,事情开始发生。

The query should be rewritten like: 该查询应重写为:

SELECT
  COUNT(*)
FROM `stats`
WHERE `stats`.`time` <= DATE_ADD(NOW(), INTERVAL 1 DAY)
  AND `stats`.`time` >= DATE_SUB(NOW(), INTERVAL $timeframe DAY)

You could do something like this: 您可以执行以下操作:

function getScans($timeframe = 0)
{
    if ($timeframe != 0) { 
        $query = 'SELECT COUNT( * ) 
            FROM stats 
            WHERE time <= DATE_ADD(NOW(), INTERVAL 1 DAY) 
            AND time >= DATE_SUB(NOW(), INTERVAL ' . ($timeframe - 1) . ' DAY)';
    } else {
        $query = 'SELECT COUNT( * ) 
            FROM stats';
    }

    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    return $row[0];
}

But it isn't the safest approach if $timeframe could be supplied by a user or the like. 但是,如果$timeframe可由用户等提供,那不是最安全的方法。 Rather, you should consider using something like: 相反,您应该考虑使用类似以下内容的方法:

$query = sprintf ('SELECT
  COUNT(*)
FROM `stats`
WHERE `stats`.`time` <= DATE_ADD(NOW(), INTERVAL 1 DAY)
  AND `stats`.`time` >= DATE_SUB(NOW(), INTERVAL %d DAY)',
mysql_real_escape_string ($timeframe, $connection_handle));

or, better yet, search SO for tips on sanitizing user input before adding it to an SQL statement. 或者更好的方法是,在SO上搜索有关清理用户输入的提示,然后再将其添加到SQL语句中。

下面的单引号不是必需的,它破坏了该功能的其余部分。

.$timeframe - 1';

the format of curdate and timeframe might not be what you expect so the calculation isn't working. 时钟和时间范围的格式可能与您预期的不符,因此计算无法正常进行。 write the query in your phpmyadmin or equivalent and work out if there are syntax errors first and wether the output is as you expect 用phpmyadmin或同等语言编写查询,然后首先确定是否存在语法错误,然后输出是否如您所愿

echo out the values and see the differences in PHP. 回显值并查看PHP中的差异。 then use these in your query. 然后在查询中使用它们。

correct result? 正确的结果?

hope this helps 希望这可以帮助

Hm i would rather do "all the logic" with PHP and then just run a pretty basic MySql query. 嗯,我宁愿使用PHP执行“所有逻辑”,然后只运行一个非常基本的MySql查询。 Example you can get a timestamp (Ymd H:i:s) for dates in a past easily with this: 例如,您可以使用以下方法轻松获取过去日期的时间戳记(Ymd H:i:s):

$dayago = date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")-1,date("Y")));
$weekago = date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")-7,date("Y")));
$monthago = date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")-30,date("Y")));

It is also the same with the future dates so examples: 将来的日期也一样,因此示例如下:

$tomorrow = date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")+1,date("Y")));

So i will do all the logic with PHP get 2 dates and then run a Mysql query. 因此,我将使用PHP进行所有逻辑处理,以获取2个日期,然后运行Mysql查询。 Something like: 就像是:

$count = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM stats WHERE date>='$weekago' AND date<='$tomorrow'"),0);

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

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