Heya, ataulhaq.
The issue with doing this using a query is that you have to take into account the number of Users who were logged in before you start calculating.
For example, assume 2 Users logged in at 2007-05-31 13:45:00 and 2007-05-31 13:45:05, respectively.
Here's where the problem occurs:
-
SELECT
-
.
-
.
-
.
-
WHERE
-
`datetime` BETWEEN '2007-06-01 00:00:00' AND '2007-06-30 11:59:59'
-
Notice that the number of active Users on June 1st will be at least two off because it won't take into account the two Users who logged in on May 31st without logging off.
And if the Users that were logged in on May 31st then log out on June 1st, your User count might even be negative!
I think your best bet is to either update a counter on login/logout, or you can do what phpBB does, but you'll have to combine it with a periodic script (such as cron, or by implementing a cron script and hope your traffic is frequent).
The way phpBB does it is the database stores the timestamp of the last action taken by the User (i.e., every time the User sends a request, the timestamp updates).
Then, to find out the number of *active* Users, simply count the number of timestamps greater than 15 minutes ago.
To get a history of this value, you'd need to run a script once per day and count (and index) the number of timestamps greater than 1 day ago.
The counter idea is probably easier, though the 'active' counter is probably a better benchmark.