简体   繁体   中英

Checking to see if a DATETIME was 14 days ago

What I'm trying to do is query a table in the database so that I can send a Welcome email 14 days after they have activated their account...

Once I successfully send the email, I will then set the flag = 0 so that the email isn't sent again in the future.

I don't know how to compare the activation_date DATETIME to the 14 day period...

you can use datediff:

if the datediff to the activation = 14, select the row:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

WHERE 14 = DATEDIFF(activation_date, $today) 

您可以在查询中将您的Activation_date与DATE_SUB(NOW(),INTERVAL 14天)进行比较。

Use datediff() function, here is an example to see how it works: http://sqlfiddle.com/#!2/a2581/7851

Select '2013-04-10', 

case 
when datediff( now() , '2013-04-10' ) = 14 
then 'Send eMail' 
else 'Don''t send eMail' 
end 

as SendeMail

UNION ALL 


Select '2013-04-11', 

case 
when datediff( now() , '2013-04-11' ) = 14 
then 'Send eMail' 
else 'Don''t send eMail' 
end 

as SendeMail

UNION ALL


Select '2013-04-15' , 

case 
when datediff( now() , '2013-04-15' ) = 14 
then 'Send eMail' 
else 'Don''t send eMail' 
end 

as SendeMail

Of course you can use it in a WHERE Statement ...

WHERE datediff( now() , '2013-04-11' ) = 14 

Use now() function to get the current date. My example date (2013-04-11) should be replaces with your activation_date column.

You can use the date_sub function in a where clause

where activation_date > date_sub(now() ,interval 14 day) and flag <> 0

sample code here

SELECT * FROM my_table WHERE DATEDIFF( NOW() , activation_date ) = 14 AND active = 1 AND msg_welcome = 0

This is the query that I needed! Thanks a lot guys... just had to modify your advice a little.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM