PHP time elapsed with MySQL DateTime - php

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.

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");

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))
{
.....
}

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))
{
.....
}

Related

PHP compare time from SQL database with current time

I would like to check if a time in an SQL database is more than a day old.
I am able to read the datetime off the SQL row and get the current time. What I am unable to do, however, is add 1 day/24 hours to the time pulled from the row, and comparing the 2.
$currenttime = date("Y-m-d h:m:s");
$tmstp = $row['datetime'];
$newtime = date("Y-m-d h:m:s", $tmstp);
echo $tmstp."<br>".$currenttime."<br>".$newtime;
returns the following:
Notice: A non well formed numeric value encountered in C:\xampp\htdocs\test.php on line 12
0000-00-00 00:00:00
2016-03-12 09:03:39
1970-01-01 01:01:00
the null time is to be expected, I initialized the row but didn't pass a value to it. Setting it to anything other than null(including setting it to an arbitrary date more than 24 hours ago but after 1970-1-1 OR adding one hour to it:
$currenttime = date("Y-m-d h:m:s");
$tmstp = $row['datetime'] + strtotime('+ 24 hours');
$newtime = date("Y-m-d h:m:s", $tmstp);
echo $tmstp."<br>".$currenttime."<br>".$newtime;
sets it to tomorrow at this time:
1457903185
2016-03-12 10:03:25
2016-03-13 10:03:25
Once I can get the expected values, how would I effectively compare the 2 values?
Thanks for any help
For compare times you can use the strtotime like this:
$tomorrow = strtotime('+ 24 hours');
$timestamp = strtotime($row['datetime']);
if ($timestamp > $tomorrow) {
// The date in database is greater than tomorrow
}

Displaying a date in php 24 hours later, issues

I'm basically trying to add 24 hours to a date with php and display it but it keeps adding only 23 hours in stead of 24 hours.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo gmdate("Y-m-d H:i:s", $set_time);
?>
So the result that I'm getting out of this is:
2015-03-19 19:03:23
but it's this what should be coming out of this:
2015-03-19 20:03:23
I'm new at working with these time functions and I can't figure out why it keeps getting adding 23 hours. Obviously I can multiply it by 25 and get 24 hours but that doesn't make sense to me.
So my question is: what's the proper way to add 24 hours to a date?
I would do it like so:
date("Y-m-d H:i:s", strtotime("+1 day"));
strtotime() uses default time zone, gmdate() uses Greenwich Mean Time (GMT). Try using date() instead.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo date("Y-m-d H:i:s", $set_time);
?>

PHP - strftime (minutes and seconds ignored)?

I tried to save time in format YYYY-mm-dd 23:59:59 to mysql database column with datetime. I don't understand why minutes and seconds are ignored always 00 ? Thank you very much for help.
PHP:
$time = strftime('%Y-%m-%d %H:%i:%s', time());
Output:
2014-07-16 11:00:00
You can simple use:
$time = date("Y-m-d H:i:s");
or even better use mysql NOW() function
you have to specify timezone as follows.
date_default_timezone_set('Australia/Melbourne');
$date = date('m/d/Y h:i:s a', time());
echo $date;
The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_set before the date() or time() functions.
you can use mysql function
now()
You are looking for
$time = strftime('%Y-%m-%d %H:%M:%S', time());
from the documentation
%S Two digit representation of the second
%M Two digit representation of the minute 00 through 59
Also, what PHP version are you using? %i:%s should not be 00:00

Comparing date("Y-m-d h:i:s");

how can I make a conditional statement like if date("Y-m-d h:i:s"); is more than 30 seconds after date("Y-m-d h:i:s");.
I've previously used something like date("Y-m-d h:i:s"); < date("Y-m-d h:i:s"); + 30, however this doesn't seem to work.
Help?
Use strtotime() to convert them to UNIX time.
E.g.:
if(strtotime($date2) - strtotime($date1) > 30) {
// $date2 is more than 30 seconds after $date1
}
or
if(abs(strtotime($date2) - strtotime($date1)) > 30) {
// $dates are more than 30 second apart
}
Use strtotime() to convert both dates to a unix timestamp, then add the wanted amount of seconds to the second and do an integer comparison.
Use unixtime, instead. Just convert your date("Y-m-d h:i:s"); to date("U") or just time().
strototime() could help you with this.
It shows the time in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Then this method $unixtime + 30 will work fine as well ;)
It looks like you are trying to compare database date fields (like mysql DATETIME). Use the database systems date and time functions to compare date values (like DATE_ADD() or + and with simple < or > in MySQL).

How to get duration in terms of minutes by subtracting a previous time stamp from the present time in PHP?

How to get duration in terms of minutes by subtracting a previous time stamp from the present time in PHP?
The format of time stamp is like
2009-12-05 10:35:28
I want to calculate how many minutes have passed.
How to do it?
To do this in MySQL use the TIMESTAMPDIFF function:
SELECT TIMESTAMPDIFF(MINUTE, date_lastaccess, NOW()) FROM session;
Where session is the table and date_lastaccess is a DATE / DATETIME field.
If you don't wanna add a library, you can do this pretty easily:
<?php
$date1 = "2009-12-05 10:35:28";
$date2 = "2009-12-07 11:58:12";
$diff = strtotime($date2) - strtotime($date1);
$minutes_passed = $diff/60;
echo $minutes_passed." minutes have passed between ".$date1." and ".$date2;
?>
Check out some pretty date libraries in PHP. Might have something for you. Here's one : http://www.zachleat.com/web/2008/02/10/php-pretty-date/
strtotime("now") - strtotime("2009-12-05 10:35:28")
That will get you seconds difference. Divide by 60 for minutes elapsed. If you have any information on the time zone of the initial timestamp, however, you'd need to adjust the "now" to a more comparable time
something like that
$second = strtotime('now') - strtotime($your_date);
$minute = $second / 60;
strtotime return the number of seconds since January 1 1970 00:00:00 UTC, so you can easily manipulate this. if you need don't want to have 35.5 minute you can use floor to round up the number. Last remark both time need to be in the same timezone or you are going to count the timezone difference also.
You could just use
$timestamp = time();
$diff = time() - $timestamp
$minutes = date( i, $diff );
function timeAgoMin($timestamp){
if(!is_int($timestamp)){
$timestamp = strtotime($timestamp);
}
return ((time() - $timestamp) / 60);
}

Categories