Reputation: 2220
Good day all, I'm developing a small script that has to check if an entry from a MySQL database is at least older than 10 days from now. the table has a timestamp field (which is the one I would like to use, but I can change it if needed). Actually I'm using this sql string:
SELECT *
FROM entries
WHERE tag='$tag'
AND DATE(last_update) > DATE_SUB(CURDATE(), INTERVAL 10 DAY)
but I'm discovering some selected entries "younger" than 10 days so maybe I'm mistaking something. is this the right way or is better to check it via PHP (and not with the MySQL query) ?
thanks in advance.
Upvotes: 6
Views: 17424
Reputation: 2017
You can also use DATEDIFF():
SELECT *
FROM entries
WHERE tag='$tag'
AND DATEDIFF(CURDATE(), DATE(last_update)) > 10
Upvotes: 2
Reputation: 54
You could also consider your query and add a negative interval value:
SELECT *
FROM entries
WHERE tag='$tag'
AND DATE(last_update) > DATE_SUB(CURDATE(), INTERVAL -10 DAY)
Upvotes: 0
Reputation: 6065
older than 10 days
should be before
the calculated date.
SELECT * FROM entries
WHERE tag='$tag' AND DATE(last_update) < DATE_SUB(CURDATE(), INTERVAL 10 DAY)
Upvotes: 30