CH

I cant seem to find the correct query to select the timestamps from a database that are less then 5 minutes old.

add a comment
0

6 Answers

  • Votes
  • Oldest
  • Latest
CH

For anyone that cares

SELECT *
FROM my_table 
WHERE
my_timestamp > DATE_SUB(now(), INTERVAL 5 MINUTE)

grabs whatever rows that contain a timestamp to my_timestamp in the last 5 minutes.

add a comment
0
JO
NOW() - 300

doesn't to it for you ? 🤔

add a comment
0
CH

no i didnt get any results using now() -x number

add a comment
0
JO

Ah, NOW returns a non-UNIX timestamp.

UNIX_TIMESTAMP() - 300

And come to think of it, you're probably not storing timestamps as UNIX timestamps are you ?

add a comment
0
BP
SELECT * FROM my_table WHERE my_timestamp > now() - INTERVAL 5 MINUTE
add a comment
0

Ah, NOW returns a non-UNIX timestamp.

UNIX_TIMESTAMP() - 300

And come to think of it, you're probably not storing timestamps as UNIX timestamps are you ?

In that case you could do something like the following:

$5_minutes_ago = date("Y-m-d H:i:s",mktime(date("H"),(date("i")-5),date("s"),date("m"),date("d"),date("Y")));
$sql = "SELECT * FROM my_timestamp > " . $5_minutes_ago;

remember to set the date format n the first variable according to the one you are using in your database ... use this as a reference ...

add a comment
0