Question

I'm using a MySQL database, accessing it through PHP, and I want it to not show older entries then today's date. I had a heck of a time getting it to order them. I had it working before, now it's not. I have them ordered by ASC, by date, fine, but it's still showing older entries. I know my PHP is outdated, but any method will work for me. Here's my code.

Sorry if I come off as a complete ditz.. I come here if I don't know, and I'm relatively new to this, and always get hung up on dates.

So far it's been suggested to: Change the name of the "date" column. "date" is stored as datetime, not a string, so to change that to int

// this is where its set to show today and future dated entries
$sql = "SELECT * FROM $db WHERE date >= '$today' ORDER BY date ASC" or die(mysql_error());

$getit = mysql_query ( $sql, $conn );

print "<br><b><font size=4>Appointments Coming Up</b></font>";
while($row = mysql_fetch_array($getit, MYSQL_ASSOC))
{
print "<br>";


$row[date] = date("D-M-d-Y", strtotime($row[date]));
print "<br><font size=3><b>{$row['doctor']} at {$row['time']} {$row['ampm']} on      $row[date]</font>";

}

Solved All I did was change the date column in the database to "dateappt" and it worked. Thanks for the help everyone.

Was it helpful?

Solution

Firstly "Date" is a reserved word (case sensitivity is dependant) so you should change the name of this column.

MySQL only uses 'YYYY-MM-DD' format for dates so your formats must match this (your $today).

http://dev.mysql.com/doc/refman/5.0/en/using-date.html

It may be better for you to use timestamps.

There are other ways of manipulating dates though: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

OTHER TIPS

You should check you type of field "date" in mysql,if it is datetime, you change one to "int"

This answer is assuming you stored your dates as strings since that seems to be the likely culprit.

When your dates are stored as strings (char(), varchar()), and not actual dates (date, datetime). This means they will be sorted as strings and not dates. So 12-31-2009 comes after 01-01-2014 because MySQL sees the first character of "0" and places it before date that start with "1". When you use the proper data type for your date values, MySQL then sorts them as dates and not strings and then 12-31-2009 comes before 01-01-2014.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top