Vivid Lust Posted October 21, 2009 Share Posted October 21, 2009 Could anyone tell me how I could select records from the last 7 days which are stored in a MySQL database? I have a column named "time" using the php time() fuction. Could someone tell me the sql needed to do this? thanks, Jake. Quote Link to comment https://forums.phpfreaks.com/topic/178495-solved-sort-for-last-7-days/ Share on other sites More sharing options...
Vivid Lust Posted October 21, 2009 Author Share Posted October 21, 2009 any ideas? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/178495-solved-sort-for-last-7-days/#findComment-941340 Share on other sites More sharing options...
Daniel0 Posted October 21, 2009 Share Posted October 21, 2009 Get the timestamp from seven days ago and select all rows with a timestamp greater than or equal to what you just computed. Quote Link to comment https://forums.phpfreaks.com/topic/178495-solved-sort-for-last-7-days/#findComment-941349 Share on other sites More sharing options...
Baronen Posted October 21, 2009 Share Posted October 21, 2009 SELECT * FROM tbl WHERE DATE_FORMAT(datefield, '%Y-%m-%d') > CURDATE() - INTERVAL 7 DAY This is one way, i think it should work, not tested though Quote Link to comment https://forums.phpfreaks.com/topic/178495-solved-sort-for-last-7-days/#findComment-941353 Share on other sites More sharing options...
Vivid Lust Posted October 21, 2009 Author Share Posted October 21, 2009 Hi, the query is returning 0 rows select DISTINCT `URL` from hits WHERE `cat`="language" AND DATE_FORMAT(`Time`, '%Y-%m-%d') > CURDATE() - INTERVAL 7 DAY here is an example value of Time: 1256130486 Hope someone can help Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/178495-solved-sort-for-last-7-days/#findComment-941405 Share on other sites More sharing options...
Daniel0 Posted October 21, 2009 Share Posted October 21, 2009 Get the timestamp from seven days ago $sevenDaysAgo = strtotime('-7 days'); and select all rows with a timestamp greater than or equal to what you just computed. $query = 'SELECT DISTINCT `URL` FROM hits WHERE `cat`="language" AND `Time` >= ' . $sevenDaysAgo; $result = someSortOfFunctionThatExecutesQueries($query); Quote Link to comment https://forums.phpfreaks.com/topic/178495-solved-sort-for-last-7-days/#findComment-941431 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.