简体   繁体   中英

Comparing current DateTime to a value in MySQL

I've been trying to add in an expiry for password reset requests.

When the user requests a password request at the moment, it stores an expiration date in the database as NOW() + INTERVAL 2 DAY - so exactly 2 days from now.

When the user comes back to the page by following their reset link, it checks the reset key exists and then runs a series of if statements, one of which I want to test whether the current DateTime is greater than that of which is in the database and if it is then display an error message.

I've tried a raft of ways as most tend to suggest using SQL to evaluate this. The most recent and most popular from what I've found was to do the following:

"SELECT *, DATE(`Expiration`) > DATE(NOW()) AS is_current FROM password_reset WHERE Reset_link='$key'"

This way you could then feed the results in to a variable and test it in the if statement as is_current would either be 0 or 1, ie:

if ($result_check_row['is_current']){
         echo '<div class="warning">Your password reset request has now expired. Please request a new one by completing the form again.''</div>';
     }

I haven't had any luck with this, or any of the other things I've tried which include creating a timestamp via PHP to test against this.

Can anyone see where I am going wrong or suggest a good alternative?

试试这个查询,它将产生当前日期和到期日期之间的日期差...

SELECT DATEDIFF(DATE(`Expiration`), DATE(NOW())) AS DiffDate FROM password_reset WHERE Reset_link='$key'"

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