travelkind Posted May 17, 2012 Share Posted May 17, 2012 I was wondering if there was a way to have the MAX function NOT return a Date that is more than 2 days into the future (from the current day)? If there is a Date that is more than 2 days into the future I would like to return the one closest to the current day. Here is the code I have: <?php mysql_connect("local", "xxx", "xxx") or die(mysql_error()); mysql_select_db("pricelink") or die(mysql_error()); // Get a specific result from the "ft9_fuel_tax_price_lines" table $query ="SELECT ItemNumber,TableCode,Cost, MAX(`Date`) as `max_date`, MAX(`Time`) as 'max_time' FROM `ft9_fuel_tax_price_lines` GROUP BY `ItemNumber`,`TableCode`"; $result = mysql_query($query) or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>ItemNumber</th> <th>TableCode</th> <th>Date</th> <th>Time</th> <th>Cost</th> </tr>"; // keeps getting the next row until there are no more to get while($row=mysql_fetch_array($result)) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['ItemNumber']; echo "</td><td>"; echo $row['TableCode']; echo "</td><td>"; echo $row['max_date']; echo "</td><td>"; echo $row['max_time']; echo "</td><td>"; echo $row['Cost']; echo "</td></tr>"; } echo "</table>"; ?> Any help would be appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/262689-maxdate-not-to-exceed-2-days-into-the-future/ Share on other sites More sharing options...
mrMarcus Posted May 17, 2012 Share Posted May 17, 2012 How are you storing `Date` in your table? What is the column type? Quote Link to comment https://forums.phpfreaks.com/topic/262689-maxdate-not-to-exceed-2-days-into-the-future/#findComment-1346403 Share on other sites More sharing options...
Barand Posted May 17, 2012 Share Posted May 17, 2012 SELECT MAX(date) .... WHERE date < CURDATE() + INTERVAL 2 DAY Quote Link to comment https://forums.phpfreaks.com/topic/262689-maxdate-not-to-exceed-2-days-into-the-future/#findComment-1346405 Share on other sites More sharing options...
travelkind Posted May 17, 2012 Author Share Posted May 17, 2012 How are you storing `Date` in your table? What is the column type? The column type in MYSQL is "DATE" format. Quote Link to comment https://forums.phpfreaks.com/topic/262689-maxdate-not-to-exceed-2-days-into-the-future/#findComment-1346421 Share on other sites More sharing options...
mrMarcus Posted May 17, 2012 Share Posted May 17, 2012 How are you storing `Date` in your table? What is the column type? The column type in MYSQL is "DATE" format. Just checking. Barand's code will do the trick. Edit: obviously replacing .... with your subsequent code as well as adding in your additional GROUP BY and such Quote Link to comment https://forums.phpfreaks.com/topic/262689-maxdate-not-to-exceed-2-days-into-the-future/#findComment-1346425 Share on other sites More sharing options...
travelkind Posted May 18, 2012 Author Share Posted May 18, 2012 Yes his code did work! Thanks MrMarcus and Barand! Quote Link to comment https://forums.phpfreaks.com/topic/262689-maxdate-not-to-exceed-2-days-into-the-future/#findComment-1346495 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.