0

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?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
serentei
  • 57
  • 1
  • 1
  • 5
  • Have you at least tried to [read the documentation](http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff)? – lanzz Jun 15 '12 at 19:56
  • possible duplicate of [Time difference between a Date and current Time?](http://stackoverflow.com/questions/7807241/time-difference-between-a-date-and-current-time) – Scott Saunders Jun 15 '12 at 19:56
  • @lanzz I want to do this in entirely PHP if possible. There are many columns in the table I'm working with, and I'm selecting them all with select * from....; if I use select timediff(...) as `oeu`,.... I feel that will result in a rather ugly code – serentei Jun 15 '12 at 20:01
  • @serentei:timespan between two times means difference between times? – SureshKumar Vegesna Jun 15 '12 at 20:21
  • @Suresh yes, this is what I meant – serentei Jun 15 '12 at 20:56

2 Answers2

2

In SQL also SELECT NOW() as current_timestamp (alongwith your other selections).

In PHP:

if ((strtotime($row['current_timestamp']) - strtotime($row['lastAccess15m'])) > 900) {

 // your code 
}

strtotime converts TimeStamp to seconds, and then you can compare time difference to 900 seconds. (15 minutes).

Hrishikesh
  • 1,076
  • 1
  • 8
  • 22
  • to get the current time in seconds in PHP, you can call `time()` - no need to do it in SQL just to get it to PHP. – Scott Saunders Jun 15 '12 at 20:09
  • Current time of php time() and the value on SQL entry might not be same? Especially if php file is making connection to remote SQL. By using NOW() in SQL, you are using same time frame as that used on the 'lastAccess15m' field. (I think so at least). – Hrishikesh Jun 15 '12 at 20:14
2

PHP time():

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

MySQL datetime:

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

To compare both of them, you need to convert one and them perform the comparison:

$phpDateFromMysql = strtotime( $mysqldate );

$mySqlDateFromPHP = date('Y-m-d H:i:s', $phpdate);

The easiest way is to convert the Mysql value to time() and perform the comparison:

See this working Example!

<?php

// compare if the value from $lastAccess15 is +15min in relation to the current time
$lastAccess15 = '2012-06-01 00:00:00';

$phpDateFromMysql = strtotime( $lastAccess15 );

if ($phpDateFromMysql - $lastAccess15 > 900) {
    $lastAccess15 = date('Y-m-d H:i:s', time());
}

echo $lastAccess15;
?>

Note: Inside the if statement you can perform the remaining of your operations, and even add an else clause 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

Zuul
  • 16,217
  • 6
  • 61
  • 88