Web Hosting Talk







View Full Version : Selecting from database, limit by time


P-nut
09-30-2007, 11:35 AM
I have a section in my script that allows admins to view logs. I want to give them the option to view certain time periods of the logs, ie one hour, day, or week.

I'm having a bit of trouble determining how to phrase the query; I thought about ordering by date descending (thus making the first row the most recent), but am not sure if this is the best way to go about it, nor can I determine how to limit by the time (in the hour example, 3600 seconds). My goal is to select X timeframe from the database itself and display the results.

The field to use to limit by time is named date.

Any help is always appreciated! :)

Steven
09-30-2007, 12:27 PM
How is the date saved in the db? What format

P-nut
09-30-2007, 12:39 PM
Unix timestamp; eg, 1191080691

Steven
09-30-2007, 01:09 PM
something like this?

select * from table where date < number and date > second number

P-nut
09-30-2007, 02:15 PM
Sort of, but not exactly.

I want to:
1. Select all rows from the log table that match the admin id specified
2. From those rows I want to return only those within the specified timeframe. For example, say they last logged in at 8:00 and out at 10:00. I want to retrieve the log from 9:00-10:00.

So I guess the first thing I'd need is the largest timestamp as that would be the most recent record, and then count back by the specified timeframe (hour, day, etc)

Hope this makes more sense. :)

Steven
09-30-2007, 02:27 PM
Sort of, but not exactly.

I want to:
1. Select all rows from the log table that match the admin id specified
2. From those rows I want to return only those within the specified timeframe. For example, say they last logged in at 8:00 and out at 10:00. I want to retrieve the log from 9:00-10:00.

So I guess the first thing I'd need is the largest timestamp as that would be the most recent record, and then count back by the specified timeframe (hour, day, etc)

Hope this makes more sense. :)

I'm a little tired otherwise i would create it for you, maybe a programmer can jump in but I can give some hints.

select * from table where admin_id = '1' and date < time() and date > strtotime("-1 day")

its not syntax correct but shows what i mean, shows all from yesterday to now. Maybe you can build off of it. Just have to create timestamps. mktime for example.

http://us.php.net/manual/en/function.mktime.php

P-nut
09-30-2007, 03:18 PM
You've been most helpful, Steven; thank you :) It's definitely more of a starting point than I had an hour ago.

I'll give it a go and see if I can get it to do what I want it to do.

evaxsoft
10-01-2007, 01:52 AM
there are exists usefukk between statement in where clause
For e.g SELECT * from table where date between 123 AND 1234;
This will select all records that you need.

juangake
10-01-2007, 08:38 AM
there are exists usefukk between statement in where clause
For e.g SELECT * from table where date between 123 AND 1234;
This will select all records that you need.

Yeah, I'm using the BETWEEN method with "timestamp" (mysql field type) too and it works perfect.

Steven
10-01-2007, 07:33 PM
good tips.