jmaccs64 Posted August 28, 2011 Share Posted August 28, 2011 I am hoping this would return all rows within the last 30 seconds... timestamp is in this format 2011-08-26 07:26:23 SELECT id FROM idtrack WHERE NOW() > timestamp > (DATE_SUB(NOW(), INTERVAL 30 SECOND)) its not... Thanks-In-Advance! Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/ Share on other sites More sharing options...
Pikachu2000 Posted August 28, 2011 Share Posted August 28, 2011 Why are you using two consecutive greater than comparisons like that? Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1262855 Share on other sites More sharing options...
cocoscrappy Posted August 28, 2011 Share Posted August 28, 2011 Try this: $off = strtotime("-30 seconds"); $time = date("Y-m-d H:i:s", $off); $res = mysql_query("SELECT id FROM idtrack WHERE timestamp > '$time'"); Not tested... Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1262858 Share on other sites More sharing options...
gizmola Posted August 29, 2011 Share Posted August 29, 2011 What mysql data type is timestamp? Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1263020 Share on other sites More sharing options...
fenway Posted August 29, 2011 Share Posted August 29, 2011 You want: SELECT id FROM idtrack WHERE timestamp >= NOW() - INTERVAL 30 SECOND Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1263151 Share on other sites More sharing options...
The Little Guy Posted August 29, 2011 Share Posted August 29, 2011 fenway, is that the same as this? SELECT id FROM idtrack WHERE timestamp >= DATE_SUB(NOW(), INTERVAL 30 SECOND); if so, which is faster? Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1263205 Share on other sites More sharing options...
AyKay47 Posted August 29, 2011 Share Posted August 29, 2011 same thing.. different way of writing it.. preference Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1263206 Share on other sites More sharing options...
fenway Posted August 29, 2011 Share Posted August 29, 2011 same thing.. different way of writing it.. preference Actually, they're not the same thing with invalid dates. Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1263296 Share on other sites More sharing options...
AyKay47 Posted August 30, 2011 Share Posted August 30, 2011 same thing.. different way of writing it.. preference Actually, they're not the same thing with invalid dates. are you saying that they handle invalid dates differently..? Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1263460 Share on other sites More sharing options...
fenway Posted September 1, 2011 Share Posted September 1, 2011 That's exactly what I'm saying -- it's undocumented, and it's evil. Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1264290 Share on other sites More sharing options...
AyKay47 Posted September 1, 2011 Share Posted September 1, 2011 his method is absolutely documented.. Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1264296 Share on other sites More sharing options...
fenway Posted September 1, 2011 Share Posted September 1, 2011 his method is absolutely documented.. The difference between the functions on invalid dates isn't. Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1264311 Share on other sites More sharing options...
The Little Guy Posted September 1, 2011 Share Posted September 1, 2011 I'm sorry, but which one then supports the invalid dates? Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1264359 Share on other sites More sharing options...
gizmola Posted September 1, 2011 Share Posted September 1, 2011 I think fenway is just pointing out that the mysql date functions will do some strange things when dealing with invalid dates, and this isn't necessarily documented. I've done a lot of looking at mysql date handling and what I found is that mysql comes in a mode where it will accept invalid dates in many cases. This is actually by design, and can be reconfigured in the latest version of mysql. For example, you can store a datetime that has zeros in it, or a valid year and invalid month/day or any variation. You can now change the configuration of mysql so that it does not allow invalid dates, but that is not the default, nor have I personally played with the setting. In this particular case, the point is moot, because the parameter NOW() will always be valid. One other thing about DATE_SUB, is that it is fairly new function, whereas DATE_ADD has been available for a long time. You can achieve the same effect with DATE_ADD by passing negative interval values ie. (INTERVAL -30 SECOND), and I always use DATE_ADD for that reason. In conclusion: the OP has not been seen in this thread for a while. I never got an answer on whether or not the column in question is a datetime, an integer or a mysql timestamp. That is an important piece of information. Regardless, he seems no longer to care about this thread, so let's let it go off to sleep. Quote Link to comment https://forums.phpfreaks.com/topic/245867-now/#findComment-1264455 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.