0

I am writing a login system in PHP and would like a to implement a function that checks if the 15 minutes has elapsed since the last failed login attempt.

In the MySQL database in my users table there is a column called last_login_datetime which is defined as SQL's DATETIME format, ex. 2018-02-24 21:12:57.

This is the code I am using to check if 15 minutes has elapsed since the last login fail

$dateTime = date("Y-m-d H:i:s");

if($row['user_locked']==$statusY AND $dateTime - strtotime($row['last_loginfail_datetime'] > 15 * 60))
{
.....
}

This does not seem to work for me at the moment and was hoping if someone could point out where I am going wrong?

Thanks in advance.

4
  • Explain to us the exact error you are getting, or the output you are getting. Commented Feb 24, 2018 at 20:30
  • Not getting any errors, it is just that it always returns as true Commented Feb 24, 2018 at 20:32
  • I have posted an answer. Please check if it works for you. Commented Feb 24, 2018 at 20:34
  • An upvote would be nice. Thanks Commented Feb 24, 2018 at 20:45

3 Answers 3

4

It doesn't work because you are substracting unix time from a string date. What you need to do is to get $dateTime as unix time.

$dateTime = strtotime("now");
Sign up to request clarification or add additional context in comments.

Comments

1

Just use strtotime($dateTime) instead of $dateTime in the if condition. strtotime returns a UNIX tiemstamp i.e: the number of seconds since January 1 1970 00:00:00 GMT $dateTime = date("Y-m-d H:i:s");

if($row['user_locked']==$statusY AND strtotime($dateTime) - strtotime($row['last_loginfail_datetime'] > 15 * 60))
{
.....
}

Comments

0

Your code won't work because your date will be read as a string. Try this instead:

$dateTime = strtotime("now");

if($row['user_locked']==$statusY AND $dateTime - strtotime($row['last_loginfail_datetime'] > 15 * 60))
{
.....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.