TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-08-2008, 03:37 PM   #1 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Confused MySQL Time +24 hours

Hello,

I have a MySQL timestamp column in my table, problem is, i want to see if any are older than 24 hours, how would i be able to do that with the MySQL timestamp?

Thanks.
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 02-08-2008, 04:21 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Code:
SELECT fields FROM table WHERE ts_field >= ts_field+(60*60*24)
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
WinSrev (02-08-2008)
Old 02-08-2008, 04:21 PM   #3 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Application

sql Code:
SELECT * FROM `my_table` WHERE TIME_TO_SEC(TIME()) - TIME_TO_SEC(`access`) < (60*60*24);
Unless I'm mistaken, or have mistyped something, I'm sure this is what you're after. The timestamps are stored in seconds, though not a strictly integer format mind you, so convert to seconds and compare the difference. This won't work for times in the future versus the current time though, as they'll always turn out to be negative.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
The Following User Says Thank You to RobertK For This Useful Post:
WinSrev (02-08-2008)
Old 02-08-2008, 04:24 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

There are a whole bunch of functions available in MySQL to play around with dates: MySQL Date & Time Functions
Salathe is offline  
Reply With Quote
Old 02-08-2008, 04:28 PM   #5 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

Yeah, I was digging in my MySQL reference book, since Srev got me interested, and I think this would be a better method:
sql Code:
SELECT * FROM `my_table` WHERE TIMESTAMPDIFF(HOUR, `access`, TIME()) <= 24;

That makes it a whole lot simpler than what I thought of before. However it does require MySQL 5.0 and upwards.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
The Following User Says Thank You to RobertK For This Useful Post:
WinSrev (02-08-2008)
Old 02-08-2008, 04:35 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

You could also play around with DATE_ADD/SUB, for example:
SELECT DATE_SUB(NOW(), INTERVAL 24 HOUR) AS 24_hours_ago;
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
WinSrev (02-08-2008)
Old 02-08-2008, 09:01 PM   #7 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Hmm... oddly enough none of them seemed to of worked :s
I have MySQL5+ too
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 02-08-2008, 09:06 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

When you say none worked, could you provide us with some details; exact queries used, your exact MySQL version, copy/paste error messages, etc?
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Alan @ CIT (02-08-2008)
Old 02-08-2008, 09:25 PM   #9 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Well Salathe, yours thinks every date is older than 24 hours :s, any ideas?
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 02-08-2008, 09:51 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

What query are you using?
Salathe is offline  
Reply With Quote
Old 02-08-2008, 09:57 PM   #11 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

SELECT *
FROM `my_rss_feeds`
WHERE DATE_SUB( NOW( ) , INTERVAL 24 HOUR )
LIMIT 0 , 30
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 02-08-2008, 10:25 PM   #12 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

After a quick chat on IRC, we settled on using:
DATE_SUB(NOW(), INTERVAL 24 HOUR) > rsstimestamp
(where rsstimestamp is the TIMESTAMP column). :)
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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


All times are GMT. The time now is 07:50 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design