Kush Posted February 23, 2011 Share Posted February 23, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/228569-within-time-limit/ Share on other sites More sharing options...
Kush Posted February 23, 2011 Author Share Posted February 23, 2011 Anyone? :'( Quote Link to comment https://forums.phpfreaks.com/topic/228569-within-time-limit/#findComment-1178519 Share on other sites More sharing options...
BlueSkyIS Posted February 23, 2011 Share Posted February 23, 2011 what have you tried? Quote Link to comment https://forums.phpfreaks.com/topic/228569-within-time-limit/#findComment-1178528 Share on other sites More sharing options...
Kush Posted February 23, 2011 Author Share Posted February 23, 2011 what have you tried? I don't even know where to start at.. Quote Link to comment https://forums.phpfreaks.com/topic/228569-within-time-limit/#findComment-1178533 Share on other sites More sharing options...
Kush Posted February 23, 2011 Author Share Posted February 23, 2011 :'( Quote Link to comment https://forums.phpfreaks.com/topic/228569-within-time-limit/#findComment-1178583 Share on other sites More sharing options...
Kush Posted February 23, 2011 Author Share Posted February 23, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/228569-within-time-limit/#findComment-1178591 Share on other sites More sharing options...
codefossa Posted February 23, 2011 Share Posted February 23, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/228569-within-time-limit/#findComment-1178610 Share on other sites More sharing options...
Kush Posted February 23, 2011 Author Share Posted February 23, 2011 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 } Quote Link to comment https://forums.phpfreaks.com/topic/228569-within-time-limit/#findComment-1178615 Share on other sites More sharing options...
Kush Posted February 23, 2011 Author Share Posted February 23, 2011 Is this even possible? Quote Link to comment https://forums.phpfreaks.com/topic/228569-within-time-limit/#findComment-1178663 Share on other sites More sharing options...
Kush Posted February 23, 2011 Author Share Posted February 23, 2011 Since I can't edit I tried to post a new topic explaining better and I get a warn.. awesome now site admin is mad at me. :'( Quote Link to comment https://forums.phpfreaks.com/topic/228569-within-time-limit/#findComment-1178720 Share on other sites More sharing options...
AbraCadaver Posted February 23, 2011 Share Posted February 23, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/228569-within-time-limit/#findComment-1178738 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.