Jump to content

MAX(`Date`) not to exceed 2 days into the future


travelkind

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.