Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Best way to compare date/time
Old 03-23-2005, 03:10 AM Best way to compare date/time
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
Trades: 0
I need to be able to select all records with a timestamp in the last x number of hours.

There are several tasks I'm facing which require this type of comparison. In the first instance, I need to erase all user accounts that haven't been verified after 24 hours. For another table I need to do the same thing, but with 48 hours. And for the admin panel I'm building for myself, I want to see all users who have logged in in the last 24 hours.

After googling and (of course) reading the manuals I've ended up with something like this, which doesn't seem to work accurately ("timestamp" is the column name, as well as the field type):
Code:
"SELECT DISTINCT username FROM log_login WHERE timestamp > (DATE_ADD(CURDATE(), INTERVAL -24 HOUR))"
I think I just need someone to spell it out for me.

Is it best to use timestamp or datetime? Of course I'd like to do all the work with the SQL statement, and not PHP.

Eagerly awaiting assistance,
Phaedrus
__________________

Please login or register to view this content. Registration is FREE
Phaedrus is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-23-2005, 06:47 AM
Rufo's Avatar
Extreme Talker

Posts: 173
Trades: 0
Whenever you refer to the column called timestamp, I'd enclose it in backticks just so there is no confusion.

I tend to store dates in UNSIGNED INT(10)s and populate them using the PHP date() function, though this is just personal preference.
__________________

Please login or register to view this content. Registration is FREE
Rufo is offline
Reply With Quote
View Public Profile Visit Rufo's homepage!
 
Old 03-24-2005, 07:28 PM
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
Trades: 0
Wow, that was harder than I anticipated. I did finally get it, though. I used a date/time field and used this SQL statement to select records within the last 24 hours:

SELECT * FROM log_login WHERE time_field >= ADDDATE(NOW(),INTERVAL -24 HOUR)

I changed the field name, but that didn't end up having anything to do with it.

Maybe that will help someone else
__________________

Please login or register to view this content. Registration is FREE
Phaedrus is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Best way to compare date/time
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.58747 seconds with 12 queries