TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   MySQL Time +24 hours (http://www.talkphp.com/general/2210-mysql-time-24-hours.html)

WinSrev 02-08-2008 03:37 PM

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.

xenon 02-08-2008 04:21 PM

Code:

SELECT fields FROM table WHERE ts_field >= ts_field+(60*60*24)

RobertK 02-08-2008 04:21 PM

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.

Salathe 02-08-2008 04:24 PM

There are a whole bunch of functions available in MySQL to play around with dates: MySQL Date & Time Functions

RobertK 02-08-2008 04:28 PM

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.

Salathe 02-08-2008 04:35 PM

You could also play around with DATE_ADD/SUB, for example:
SELECT DATE_SUB(NOW(), INTERVAL 24 HOUR) AS 24_hours_ago;

WinSrev 02-08-2008 09:01 PM

Hmm... oddly enough none of them seemed to of worked :s
I have MySQL5+ too

Salathe 02-08-2008 09:06 PM

When you say none worked, could you provide us with some details; exact queries used, your exact MySQL version, copy/paste error messages, etc?

WinSrev 02-08-2008 09:25 PM

Well Salathe, yours thinks every date is older than 24 hours :s, any ideas?

Salathe 02-08-2008 09:51 PM

What query are you using?

WinSrev 02-08-2008 09:57 PM

SELECT *
FROM `my_rss_feeds`
WHERE DATE_SUB( NOW( ) , INTERVAL 24 HOUR )
LIMIT 0 , 30

Salathe 02-08-2008 10:25 PM

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


All times are GMT. The time now is 12:34 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0