Canman2005 Posted June 2, 2007 Share Posted June 2, 2007 Hi all I have a table which holds a bunch of dates id datefrom dateto 1 2007-02-22 2007-03-05 2 2007-05-13 2007-06-23 3 2007-05-25 2007-07-23 the `datefrom` and `dateto` are dates in the format YYYY-MM-DD, the `datefrom` means the date the event starts and the `dateto` means the date the event ends. What I want to run a query on this table and return any rows which have todays date between the 'datefrom' and the 'dateto'. Does that make much sense? Can anyone help? Thanks in advance Dave Quote Link to comment https://forums.phpfreaks.com/topic/54038-query-date-range/ Share on other sites More sharing options...
Wuhtzu Posted June 2, 2007 Share Posted June 2, 2007 If your times were unix timestamps you would be doing this: $today = time(); mysql_query("SELECT * FROM table WHERE datefrom < " . $today . " AND " . $today . "< dateto"); Now your only problem is that you haven't got timestamps but some other dateformat. You could play around with PHP's strtotime() function: http://no.php.net/strtotime but I don't know if there is a smarter way. Quote Link to comment https://forums.phpfreaks.com/topic/54038-query-date-range/#findComment-267172 Share on other sites More sharing options...
Barand Posted June 2, 2007 Share Posted June 2, 2007 SELECT * FROM table WHERE CURDATE() BETWEEN datefrom AND dateto Quote Link to comment https://forums.phpfreaks.com/topic/54038-query-date-range/#findComment-267199 Share on other sites More sharing options...
Wuhtzu Posted June 3, 2007 Share Posted June 3, 2007 Go for Barand's solution - BETWEEN is obviously smart and yyyy-mm-dd appears to be the date format of mysql I always use timestamps my self, so I would never have thought of the solution Barand did Quote Link to comment https://forums.phpfreaks.com/topic/54038-query-date-range/#findComment-267206 Share on other sites More sharing options...
Canman2005 Posted June 3, 2007 Author Share Posted June 3, 2007 Thanks everyone, that seemed to work fine. Can anyone tell me, I want to also return any rows which also start on a monday of the date that is being returned, so basically I can return things that also start that week? Does that make sense? Thanks Dave Quote Link to comment https://forums.phpfreaks.com/topic/54038-query-date-range/#findComment-267402 Share on other sites More sharing options...
dough boy Posted June 3, 2007 Share Posted June 3, 2007 # Select all events that occur between a range or start on a monday SELECT * FROM table WHERE ( NOW() BETWEEN datefrom AND dateto OR DAYOFWEEK(datefrom) = 2 ) # Select all events that occur between a range AND start on a monday SELECT * FROM table WHERE ( NOW() BETWEEN datefrom AND dateto AND DAYOFWEEK(datefrom) = 2 ) http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_dayofweek Quote Link to comment https://forums.phpfreaks.com/topic/54038-query-date-range/#findComment-267428 Share on other sites More sharing options...
Barand Posted June 3, 2007 Share Posted June 3, 2007 <?php $mon = date('Y-m-d', strtotime('this monday')); $fri = date('Y-m-d', strtotime('this friday')); $sql = "SELECT * FROM table WHERE (datefrom BETWEEN '$mon' AND '$fri') OR (CURDATE() BETWEEN datefrom AND dateto) "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/54038-query-date-range/#findComment-267444 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.