Is there a way to see if ((the current time - a timestamp in a database)>10 minutes) in php? I have a row in a database called lockout. I have the following code so far:
echo $row['lockout'];//returns 2013-05-12 22:04:17
echo date("Y-m-d H:i:s")//returns 2013-05-12 22:06:32
Furthermore does anyone have any ideas why when I insert the current Timestamp in my database, it is about a half hour off, but has the right date and year?
Use the http://www.php.net/manual/en/book.datetime.php datetime class in php This has a diff method
If your SQL dialect is MySQL use
... WHERE UNIX_TIMESTAMP(NOW())-lockout>=600 ...
or something similar in PHP.
Do this in your query:
WHERE `lockout` < DATE_SUB(NOW(), INTERVAL 10 MINUTE)
Or, if you cannot rely on MySQL's time, use
mysqli_query('
... WHERE `lockout` < DATE_SUB(FROM_UNIXTIME(' . time() . '), INTERVAL 10 MINUTE) ...
');
Or, if you are bound to date(...)
, use
mysqli_query('
... WHERE `lockout` < DATE_SUB(' . date('Y-m-d H:i:s') . ', INTERVAL 10 MINUTE) ...
');
You might want to compare MySQL's and PHP's timezones and your servers' ones (if MySQL and PHP live on separate hosts).
The SQL answer could depend on which SQL technology you use. To do this in PHP rather than SQL you could use the diff() method of DateTime:
$datetime1 = new DateTime($row['lockout']);
$datetime2 = new DateTime(date("Y-m-d H:i:s"));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%i minutes');
In PHP you can use strtotime function with relative formats like - 10 minutes
to get the desired Unix timestamp:
if ( strtotime('- 10 minutes') < strtotime( $row['lockout'] ) ) ...