简体   繁体   English

PHP ARRAY根据日期范围显示数组中的分组数据

[英]PHP ARRAY display grouped data from the array based on date ranges

I have this query 我有这个查询

$query = mysql_query ("SELECT type, count(*), date FROM tracking WHERE htcode='$htG' AND type IN ('viewed', 'shared', 'printed', 'emailed', 'used') GROUP BY type, date ORDER BY date ASC");

This code: 这段代码:

while ($result = mysql_fetch_assoc($query)){
echo $result['date'] .' / ' .$result['type'].' = ' .$result['count(*)'];
echo '<br>';
}

gives me : 给我 :

2012-02-01 / viewed = 2
2012-02-03 / emailed = 1
2012-02-04 / shared = 1
2012-02-05 / viewed = 1
2012-02-07 / viewed = 2
2012-02-07 / shared = 3
2012-02-07 / emailed = 1
2012-02-07 / printed = 1

How can i take the query array and pull all (viewed, shared, emailed, printed, used) for today, for yesterday, and for a date range (last 30 days for example)? 如何获取查询数组并提取今天,昨天和日期范围(例如最近30天)的所有(查看,共享,通过电子邮件发送,打印,使用的)?

To get all results for a specific date range, don't group by date; 要获取特定日期范围内的所有结果,请不要按日期分组; instead use a WHERE clause to limit the date of the count. 而是使用WHERE子句来限制计数的日期。 Otherwise the query is very similar. 否则查询非常相似。

$query = mysql_query("SELECT type, count(*) FROM tracking WHERE htcode = '$htG' AND type IN ('viewed', 'shared', 'printed', 'emailed', 'used') AND date >= '$start_date' AND date <= '$end_date' GROUP BY type");

Here's just the query itself formatted over several lines for clarity: 为了清楚起见,以下只是查询本身的格式:

SELECT type, count(*)
FROM tracking
WHERE htcode = '$htG'
  AND type IN ('viewed', 'shared', 'printed', 'emailed', 'used')
  AND date >= '$start_date' AND date <= '$end_date'
GROUP BY type

Just make sure you set the values for $start_date and $end_date first. 只需确保首先设置$start_date$end_date的值即可。 For example, for today you'd just set them both to this: 例如,今天您只需将它们都设置为此:

$start_date = date('Y-m-d');
$end_date = date('Y-m-d');

Note that date('Ym-d') is just today's date in a format like YYYY-MM-DD . 请注意, date('Ym-d')只是今天的日期,格式YYYY-MM-DD

For yesterday, you'd pass in yesterday's timestamp into the date() function, but use the same format: 对于昨天,您可以将昨天的时间戳传递到date()函数中,但使用相同的格式:

$start_date = date('Y-m-d', strtotime('yesterday'));
$end_date = date('Y-m-d', strtotime('yesterday'));

And for the past thirty days, you'd start 30 days ago and end today: 在过去的30天里,您将在30天前开始,到今天结束:

$start_date = date('Y-m-d', strtotime('30 days ago'));
$end_date = date('Y-m-d');

You can do it like this for any start/end date. 您可以在任何开始/结束日期这样操作。 Note that I'm using strtotime() , which lets you use a lot of English phrases to specify timestamps rather than having to do date arithmetic. 请注意,我使用的是strtotime() ,它使您可以使用许多英语短语来指定时间戳,而不必执行日期算术。

Also, since you will be doing the query multiple times, it would probably make sense to separate out the logic into its own function: 另外,由于您将多次执行查询,因此将逻辑分离为自己的函数可能很有意义:

function report_range($htG, $start_date, $end_date) {
    $query = mysql_query("SELECT type, count(*) FROM tracking WHERE htcode = '$htG' AND type IN ('viewed', 'shared', 'printed', 'emailed', 'used') AND date >= '$start_date' AND date <= '$end_date' GROUP BY type");

    while ($result = mysql_fetch_assoc($query)){
        echo $start_date . '-' . $end_date . ' / ' . $result['type'] . ' = '  . $result['count(*)'];
        echo '<br>';
    }
}

Then run it with something like this (last 30 days in this example): 然后使用以下命令运行它(在本示例中为最近30天):

report_range($htG, date('Y-m-d', strtotime('30 days ago')), date('Y-m-d'));

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

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