avo Posted January 29, 2008 Share Posted January 29, 2008 Hi All if i have lots of dates in a sql table formated like 2008-10-01 How would i write a sql function please to allow me to get the closest date to NOW() or if there is one the date in the past and forget the future date closest to NOW() Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/88483-solved-sql-statement/ Share on other sites More sharing options...
themistral Posted January 29, 2008 Share Posted January 29, 2008 Think you might need 2 queries function functionName ($now) { //get last date prior to now $query1 = mysql_query("SELECT field_date FROM table WHERE field_date < '$now' ORDER BY field_date DESC LIMIT 1"); $result1 = mysql_fetch_array($result1); // get next future date $query2 = mysql_query("SELECT field_date FROM table WHERE field_date > '$now' ORDER BY field_date ASC LIMIT 1"); $result2 = mysql_fetch_array($result2); // do a calculation to find which of $result1[field_date] and $result2[field_date] is closest to $now } Be interested to know how you get on! HTH Quote Link to comment https://forums.phpfreaks.com/topic/88483-solved-sql-statement/#findComment-452939 Share on other sites More sharing options...
avo Posted January 29, 2008 Author Share Posted January 29, 2008 Hi This looks like it is working well can anyone just have a quick look to see if im missing anything You was correct i did need two statements. This is what i ended up with //check to see if there any over due dates $date_query = mysql_query ("SELECT cal_due FROM serials WHERE cal_due < NOW() ORDER BY cal_due ASC LIMIT 1") or die (mysql_error()); $cal_due=mysql_fetch_array($date_query) ; if(empty($cal_due['cal_due'])) { //if not do the future dates and select the closest to NOW() $date_query = mysql_query ("SELECT cal_due FROM serials WHERE cal_due >= NOW() ORDER BY cal_due ASC LIMIT 1") or die (mysql_error()); $cal_due=mysql_fetch_array($date_query) ; } echo $cal_due['cal_due'] ; [code/] Quote Link to comment https://forums.phpfreaks.com/topic/88483-solved-sql-statement/#findComment-452956 Share on other sites More sharing options...
themistral Posted January 29, 2008 Share Posted January 29, 2008 Looks OK to me - any errors coming up at all or are you just asking to see if there are improvements that can be made? Quote Link to comment https://forums.phpfreaks.com/topic/88483-solved-sql-statement/#findComment-452965 Share on other sites More sharing options...
trq Posted January 29, 2008 Share Posted January 29, 2008 Better off with something like.... <?php function functionName($now) { if ($result = mysql_query("SELECT field_date FROM table WHERE field_date < '$now' ORDER BY field_date DESC LIMIT 1")) { if (!mysql_num_rows($result)) { if ($result = mysql_query("SELECT field_date FROM table WHERE field_date > '$now' ORDER BY field_date ASC LIMIT 1")) { if (!mysql_num_rows($result)) { return false; } } } } $row = mysql_fetch_assoc($result); return $row['field_date']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88483-solved-sql-statement/#findComment-452976 Share on other sites More sharing options...
avo Posted January 30, 2008 Author Share Posted January 30, 2008 Hi Thank you Appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/88483-solved-sql-statement/#findComment-453442 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.