简体   繁体   中英

Php count mysql with datetime and group

I have a mysql database that I store visitors in.

id        ip             date
--     -----------    ------------------   
1      192.168.0.1    2013-02-08 12:12:04
2      192.168.0.1    2013-02-08 14:30:00
3      192.168.0.1    2013-02-09 16:43:46
4      192.168.0.1    2013-02-10 08:35:02

This a simplified version of that i am using but the concept is the same. I want to be able to get the number of visitors and display them in a chart by time interval. I use the following array design to populate the chart, where 0,1,2,3 is the x axis and 24, 74, 26, 51 is the y axis.

 [[0, 24], [1, 74], [2, 26], [3, 52]]

I have tried to use the queries from stack question like this for instance but i cant get it to work with the Group by and that. Any help will be appreciated.

 $query = "SELECT COUNT(id) FROM  `unique_visitors_user` WHERE DATE(date) BETWEEN '2013-02-08' AND '2013-02-10' ";

Try this one:

SELECT 
    DATE_FORMAT(date, '%Y-%m-%d') Date1, COUNT(*) AS Visitors
FROM
    unique_visitors_user
WHERE
    date BETWEEN STR_TO_DATE('%Y-%m-%d', '2013-02-08') AND STR_TO_DATE('%Y-%m-%d', '2013-02-10')
GROUP BY Date1;

This one should count all visits by day

SELECT COUNT(id)
FROM `unique_visitors_user`
WHERE date >= "2013-01-01" 
AND date < DATE_ADD("2013-01-01", INTERVAL 1 DAY)
GROUP BY DAY(date)

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