I'm having some troubles calculating if time is past 15 minutes, on a date and time in my database. How can I check this?
I have this in my database (example):
2014-01-30 15:29:31
And then I get the date from PHP:
$date = date("Y-m-d H:i:s");
But how can I check if the time have passed 15 minutes, or if it's a different day?
I have this code so far:
$date = date("Y-m-d H:i:s");
$sql = "SELECT date FROM reset WHERE session_id = '".$sessid."' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$dbdate = $value->date;
$checkdate = strtotime($dbdate);
if ($checkdate - time() > 15 * 60) {
error_log("15 mins passed");
}
You can parse the date from the database and compare it.
$dbdate = strtotime($datefromdb);
if (time() - $dbdate > 15 * 60) {
// 15 mins has passed
}
And to compare the date:
if (date("Y-m-d", $dbdate) != date("Y-m-d")) {
// different date
}
I included the year and the month to also notice changes in only them (e.g. 2014-01-01
to 2014-02-01
). Use "d"
instead of "Y-m-d"
to check only the date.
Using a DateTime object:
$dateTimeObject = new \DateTime($dateString);
//and subtract using an interval
$dateTimeObject->sub(new \DateInterval("PT15M"));
$newDateString = $dateTimeObject->format("Y-m-d H:i:s");
you cans use DATE_FORMAT functions.
$actual_minute = date("i");
$data = mysql_query("SELECT DATE_FORMAT(date,'%i') minutes FROM reset WHERE session_id = '".$sessid."'");
$database_minute = $data{'minutes'};