c_pattle Posted December 12, 2010 Share Posted December 12, 2010 I want to have a feature on a website I'm building where the user can select rows from a MySQL table based on it's age. For example they could select a piece of content between 30-60 days old. I'm going to do this using the following sequel "WHERE article_date BETWEEN 2010-12-01 AND 2010-11-01" However I'm not sure how to make this work. If the date is 2010-12-12 what is the easiest way to subtract 30 days from this date? Thanks for any help. Link to comment https://forums.phpfreaks.com/topic/221426-phpmysql-date/ Share on other sites More sharing options...
Zurev Posted December 12, 2010 Share Posted December 12, 2010 Based on your format, I'm assuming the article_date is stored as a yyyy-mm-dd format, otherwise you would have to use the timestamps... Check the query part of that link http://www.roseindia.net/sql/sql-between-datetime.shtml It states: Select * From Stu_Table Where Stu_Dob Between '1984-01-01 00:00:00' And '1986-1-1 00:00:00'; Never used the between operator in SQL myself though, more on it here: http://www.w3schools.com/sql/sql_between.asp EDIT: Didn't read your post well enough, grr... $now = mktime(); $then = $now - (60*60*24*30); That would give you the timestamp of 30 days ago, so.... $thirtydaysago = date("Y-m-d", $then); So overall, here we go: $now = mktime(); $then = $now - (60*60*24*30); $thirtydaysago = date("Y-m-d", $then); $current = date("Y-m-d", $now); mysql_query("SELECT * FROM `table` WHERE `article_date` BETWEEN '$current' AND '$thirtydaysago'"); Seem like what you need? Link to comment https://forums.phpfreaks.com/topic/221426-phpmysql-date/#findComment-1146341 Share on other sites More sharing options...
c_pattle Posted December 12, 2010 Author Share Posted December 12, 2010 Yep, thats brilliant. Just what I needed. Thanks Link to comment https://forums.phpfreaks.com/topic/221426-phpmysql-date/#findComment-1146344 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.