jcstanley Posted March 1, 2007 Share Posted March 1, 2007 Hi What I am trying to do is select items from a databse but only between two dates, but I am not sure how I can make the $week variable = today + 7 days and the $month variable = today + 1 month. $today = date("Y-m-d"); $week = ? $month = ? I guess the SQL would look something like SELECT * FROM table BETWEEN $today AND $week Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/40727-increment-date/ Share on other sites More sharing options...
Snooble Posted March 1, 2007 Share Posted March 1, 2007 http://us2.php.net/date Quote Link to comment https://forums.phpfreaks.com/topic/40727-increment-date/#findComment-197130 Share on other sites More sharing options...
jcstanley Posted March 1, 2007 Author Share Posted March 1, 2007 Thanks - but which section am i meant to look at? Quote Link to comment https://forums.phpfreaks.com/topic/40727-increment-date/#findComment-197142 Share on other sites More sharing options...
Snooble Posted March 1, 2007 Share Posted March 1, 2007 Because i'm nice: Look at the mktime() part! http://us2.php.net/manual/en/function.mktime.php Sorry my fault thought i gave you the link. :S Read read read Snooble Quote Link to comment https://forums.phpfreaks.com/topic/40727-increment-date/#findComment-197150 Share on other sites More sharing options...
jcstanley Posted March 1, 2007 Author Share Posted March 1, 2007 Thanks I have managed to increment the dates and have checked that it does work, but now having trouble with the SQL This is what I have but finds no rows. $today = date('Y-m-d'); $month = date('Y-m-d', strtotime('+1 month')); . . . $query = "SELECT eventid, name, DATE_FORMAT(date, '%d %b %y') AS date, time, type, extra FROM events WHERE type = 'Race' AND date BETWEEN $today AND $month ORDER BY 'date' asc"; Not sure what has gone wrong, and yes there are events in the databse within the date range Quote Link to comment https://forums.phpfreaks.com/topic/40727-increment-date/#findComment-197174 Share on other sites More sharing options...
obsidian Posted March 1, 2007 Share Posted March 1, 2007 I wouldn't use PHP at all for this since you're using it within a MySQL query. You're much better off letting MySQL do the work for you: SELECT eventid, name, DATE_FORMAT(date, '%d %b %y') AS formatDate, time, type, extra FROM events WHERE type = 'Race' AND date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY); Notice that to run your date comparison, you have to select your DATE_FORMAT result as a different name in order to return accurate results. Quote Link to comment https://forums.phpfreaks.com/topic/40727-increment-date/#findComment-197183 Share on other sites More sharing options...
jcstanley Posted March 1, 2007 Author Share Posted March 1, 2007 Thanks that works perfectly Can the same be done with an interval of 1 Month? Quote Link to comment https://forums.phpfreaks.com/topic/40727-increment-date/#findComment-197187 Share on other sites More sharing options...
obsidian Posted March 1, 2007 Share Posted March 1, 2007 Thanks that works perfectly Can the same be done with an interval of 1 Month? Definitely. Also, to make it more readable, and as I'm sure fenway would be quick to point out, the following is cleaner: BETWEEN CURDATE() AND CURDATE + INTERVAL 7 DAY; Check out the MySQL Date and Time functions for more details about INTERVAL. Quote Link to comment https://forums.phpfreaks.com/topic/40727-increment-date/#findComment-197193 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.