Leovenous Posted July 18, 2006 Share Posted July 18, 2006 I would think this is both a common and easy question, but 2 hours of digging have not yielded any results yet, so here goes.All I want, is a simple query to get data from a table that has been modified in the last 7 days.I have a field called 'modified' that has a time stamp 0000-00-00 00-00-00. It seems to me I want a way to check the timestamps and grab which ones are less than 7 days old.So the query might look something like:[code]SELECT * FROM players WHERE modified > (SUBDATE(CURDATE(), INTERVAL '7' DAY)[/code]But obviously I'm bumping around in the dark.Any help? Quote Link to comment https://forums.phpfreaks.com/topic/14976-query-from-the-last-7-days/ Share on other sites More sharing options...
Barand Posted July 18, 2006 Share Posted July 18, 2006 one way[code]<?php$date_7 = date('Y-m-d', strtotime('-7 days'));$sql = "SELECT .... WHERE modified >= '$date_7' ";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14976-query-from-the-last-7-days/#findComment-60213 Share on other sites More sharing options...
fenway Posted July 18, 2006 Share Posted July 18, 2006 [code]SELECT * FROM players WHERE modified >= NOW() - INTERVAL 7 DAY[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14976-query-from-the-last-7-days/#findComment-60217 Share on other sites More sharing options...
Barand Posted July 18, 2006 Share Posted July 18, 2006 Shouldn't that be NOW() or CURDATE() Quote Link to comment https://forums.phpfreaks.com/topic/14976-query-from-the-last-7-days/#findComment-60220 Share on other sites More sharing options...
fenway Posted July 18, 2006 Share Posted July 18, 2006 Yup, sorry, my bad. Quote Link to comment https://forums.phpfreaks.com/topic/14976-query-from-the-last-7-days/#findComment-60229 Share on other sites More sharing options...
Leovenous Posted July 19, 2006 Author Share Posted July 19, 2006 Yep. That worked, and it makes sense. I wonder why I have not been able to find examples of queries even similar. (shrugs)Thanks a bunch guys. Here's the final query:[code]$query = "SELECT * FROM players WHERE Creator_ID LIKE '$id' AND modified >= CURDATE( ) - INTERVAL 7 DAY";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14976-query-from-the-last-7-days/#findComment-60234 Share on other sites More sharing options...
fenway Posted July 19, 2006 Share Posted July 19, 2006 Personally, I'd use NOW(), not CURDATE(), in case you ever change your modified field to be of type DATETIME, not DATE. FYI, make sure to always use the field name as the lvalue, so that an index can be used, if available. Quote Link to comment https://forums.phpfreaks.com/topic/14976-query-from-the-last-7-days/#findComment-60273 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.