Jump to content

Within Time Limit


Kush

Recommended Posts

Hi,

 

I have a timestamp mysql field that I'm trying to see if something happened within a time limit. The field is updated with the time when the action is complete so I just need to figure out how to see if it's within a time. I currently have a defined variable set titled "MAX_RESPONSE_TIME" and that's in seconds. By default "MAX_RESPONSE_TIME" is equal to 300 seconds. How can I see if a mysql timestamp field is within 300 seconds?

Link to comment
Share on other sites

For example, my current mysql timestamp field uses the NOW() function (says so in phpmyadmin) and the current value in the field is "2011-02-22 22:25:51". Now that's going to change and I'm trying to figure out if the current time is within 300 seconds of that time. How could I do this?

Link to comment
Share on other sites

I would suggest just storing the times in unix_timestamp, which would be much easier to take from.  Then you could just do the following:

 

<?php

$expire = time() - (5 * 60);	// Within 5 minutes (300 seconds)

$result = mysql_query("SELECT * FROM `table` WHERE `timestamp` > '$expire';");
while ($row = mysql_fetch_assoc($result))
{
/*
 * $row['timestamp'] would be the time of the first row taken from the database
 * After looping, it would continue on to the next one
 */
}
mysql_free_result($result);

?>

Link to comment
Share on other sites

Kira, storing the time in a unix_timestamp isn't possible. I can only store the timestamp using a mysql timestamp field using the NOW() function. I'm passing my timestamp to a function called isActive(). This is what I have so far..

 

function isActive($timestamp)
{
    // Below is equal to the int 300
    //MAX_RESPONSE_TIME
}

Link to comment
Share on other sites

If you need all records (active and not) but need to check and do something different with the active ones in your code, then select the time using:  http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp

 

$query = "SELECT *, UNIX_TIMESTAMP(time_column) AS ts FROM table_name";

 

Then:

function isActive($timestamp) {
   if($timestamp >= (time() - MAX_RESPONSE_TIME)) {
      return true;
   }
   return false;
}

 

Or if you only want active ones from the query:

 

$query = "SELECT * FROM table_name WHERE UNIX_TIMESTAMP(time_column) >= NOW() - " . MAX_RESPONSE_TIME;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.