I am working on a auto listing website and i am stuck at some logic behind how to get the dates right.
I have created a sql fiddle for the database
http://www.sqlfiddle.com/#!2/e111e/3
I want to run a cron every day at 12 AM and i want to email the users whoselistings which are going to expire (ignores already expired ones) in a day but there must be a 24 hour difference between them as I want to tell them minimum 24 hours in advance that there listing is going to be expired. Also it should select only those listings which are going to be expire after one day only and not more than 1 day.
Here is what i tried so far
SELECT DATE_FORMAT(expiry_date, '%b %d %Y %T') AS expiry_date, DATEDIFF(expiry_date, NOW()) AS days_left FROM `test` WHERE DATEDIFF(expiry_date, NOW()) = 1
but it is also selecting which are going to expire in less than 24 hours.
Please help me with it.
EDIT
///////////////////////////////////////////////////////////////
I have figured out this. Tell me guys if its correct or not.
SELECT DATE_FORMAT(expiry_date, '%b %d %Y %T') AS expiry_date, DATEDIFF(expiry_date, NOW()) AS days_left, HOUR(TIMEDIFF(expiry_date, NOW())) as hours FROM `test`
WHERE
DATEDIFF(expiry_date, NOW()) = 1 AND HOUR(TIMEDIFF(expiry_date, NOW())) > 24
MySQL’s
datediffonly takes into account the actual days, not the hours. Try to work withTO_SECONDSand take the difference as seconds.UPDATE: and this is the code which works with the fiddle: