0

I have a last_notified column on my MySQL table, data type DATETIME I want to either only select the rows where last_notified is more than n seconds old, or compare the last_notified value from the db in php.

I have tried messing around with the date object.

$d = date('Y-m-d H:i:s'); // current date and time
$dd = date('2011-09-08 10:21:34'); // this is the format used in mysql.

I know I cannot just compare them, and i'm unaware of how to add time to the date object. I have seen examples of people using something along the lines of

$t = explode(" ",$dd);
date($dd, strtotime('+30 minutes', strtotime($t[1])));

but that doesn't work . I'm just not seeing it.

3 Answers 3

2

You can use sql like this:

SELECT * FROM your_table WHERE last_notified < DATE_SUB(NOW(), INTERVAL n SECOND)

Sign up to request clarification or add additional context in comments.

1 Comment

nice xdazz! you beat me to the punch!
1

Take a look at your first snippet and date mask in first line

Y-m-d H:m:s

you use m mask twice, so it means there will be month instead of minutes. you should use i mask to specify the minutes

Comments

1
SELECT * FROM your_table WHERE last_notified < DATE_SUB(NOW(), INTERVAL n SECOND)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.