简体   繁体   English

用MySQL或PHP计算以分钟为单位的时差量?

[英]calculate time difference amount in minutes with MySQL or PHP?

I save users sing-in information using the fields below: 我使用以下字段保存用户唱歌信息:

user_id, login_date, login_time, status user_id,login_date,login_time,状态

After each attempting for sign-in, a record will insert into database using true or false status. 每次尝试登录后,一条记录将以true或false状态插入数据库。 I want to lock user if he/she has 3 failed logins in the last 3 minutes. 我想锁定用户,如果他/她在最近3分钟内有3次失败登录。 So I need to use the current time and calculate its difference with 3 last records of table in minute format. 因此,我需要使用当前时间并以分钟格式将其与表的最后3条记录进行计算。

My questions are: 我的问题是:
1- Which one is better to do this time calculation? 1-这次计算哪个更好? PHP or MySQL? PHP或MySQL?
2- Depends on your suggestion, which function is better for it? 2-根据您的建议,哪个功能更好?

Thank you. 谢谢。

Following query returns all User_Id's according your conditions: 以下查询根据您的条件返回所有User_Id:

select user_id from t 
where login_date= CURDATE() 
      and login_time>TIMESTAMPADD(MINUTE,-3,CURTIME())
      and status = 'f'
group by user_id 
having count(*)>3

Different usage. 不同的用法。 If I were you, I will record the last failed login date & time in database, such that you can fetch the last failed login time from database along with the user data from database. 如果您是我,我将在数据库中记录上次失败的登录日期和时间,以便您可以从数据库中获取上次失败的登录时间以及数据库中的用户数据。

Do the comparison in PHP or MySQL is okay. 在PHP或MySQL中进行比较是可以的。

However, you should bear in mind that MySQL server & Web server should have same timezone and time should be synchronized, or your result will not be accurate. 但是,请记住,MySQL服务器和Web服务器应具有相同的时区,并且时间应该同步,否则结果将不准确。

Considering you need to do a lot of other things aswell, I say use php. 考虑到您还需要做很多其他事情,我说使用php。 You could retrieve the last 3 records for a user when he tries to log in using 您可以在用户尝试使用登录时检索用户的最后3条记录

SELECT login_date, login_time, status FROM table WHERE user_id=$id ORDER BY login_DATE DESC LIMIT 3

Then in PHP you can do the check. 然后在PHP中可以进行检查。 If it fails you still need to inform the user he is locked, lock the user (other table?) and return false for login. 如果失败,您仍然需要通知用户他已被锁定,请锁定该用户(其他表?)并返回false进行登录。

in php you could do some checking like 在PHP中,你可以做一些检查,如

  • Are there tree attempts returned (less is no lock) 是否有返回的树尝试(少即无锁)
  • Is the last retrieved record less then 3minutes old? 最后检索的记录是否少于3分钟? (else no lock) (其他没有锁)
  • have all tree returned values a false attempt (else no lock) 让所有树返回的值都是错误的尝试(否则没有锁定)
  • etc. 等等
  1. I think MySQL is always a better solution for calculations because it is a lot faster then PHP. 我认为MySQL始终是一种更好的计算解决方案,因为它比PHP快得多。
  2. If you use timestamps you could simple do time_x - time_y to get the difference in seconds, your total calculation will be: (time_x - time_y) / 60 and your query will look something like this: 如果您使用时间戳,你可以简单的做time_x - time_y获得在几秒钟之差,你的总的计算将是:(time_x - time_y)/ 60和您的查询就会是这个样子:

    SELECT user_id, login_date, login_time, status, ((login_time-NOW()) / 60) as differenceColomn FROM usertable 选择user_id,login_date,login_time,状态,(((login_time-NOW())/ 60)作为差异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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