I’ve got 2 fields to populate: how many times the page is called per 15 minutes and per hour.
Following fields are DateTime in MySql:
$lastAccess15m=$row['lastAccess15m'];
$lastAccessHour=$row['lastAccessHour'];
And these fields are ints:
$accessPer15m=$row['accessPer15m'];
$accessPerHour=$row['accessPerHour'];
I want to compare current time with $lastAccess15, and if the time distance is larger than 15 minutes, set the value of $lastAccess15 to current time, and reset $accessPer15m, otherwise, increment $accessPer15m. The same goes for $lastAccessHour and $accessPerHour, after which those values have to be saved to mysql database.
How do I get the timespan between current time and $accessPer15m in minutes/hours?
PHP time():
MySQL datetime:
To compare both of them, you need to convert one and them perform the comparison:
The easiest way is to convert the Mysql value to time() and perform the comparison:
See this working Example!
Note: Inside the
ifstatement you can perform the remaining of your operations, and even add anelseclause to perform some operations if the time isn’t > 15min (like the increment you where referring to).Relevant reading material:
PHP strtotime function
PHP date function
PHP elseif/else if control structure