Hi Brian
The problem is you are trying to do arithmetic with strings, which is pretty hard. You could try this instead:
Use an array of month names (e.g. $months = array('', 'January', 'February', ...)).
The user submits the form which returns the month (as above). You look through your array to find out the month number, then look through MySQL for that month.
If you find it, great your script will end. If not, loop through decrementing the month each time until it reaches 0 or you find the month with any entries.
$inmonth = array_keys($months, $_POST["month"]); //could be date("F")
$inmonth = $inmonth[0];
$sql = "SELECT DISTINCT date_format(enter,'%m') AS month_formatted ";
$sql .= "FROM dates ";
$sql .= "WHERE date_format(enter,'%m') = ".sprintf("%02d", $inmonth);
//if $q_month is found then set $month
list($mymonth) = mysql_fetch_row(mysql_query($sql));
while (($mymonth != $inmonth) && ($inmonth > 0)) {
$inmonth --;
$sql = "SELECT DISTINCT date_format(enter,'%m') AS month_formatted ";
$sql .= "FROM dates ";
$sql .= "WHERE date_format(enter,'%m') = ".sprintf("%02d", $inmonth);
list($mymonth) = mysql_fetch_row(mysql_query($sql));
}
if ($mymonth == $inmonth) {
echo "Date found in $mymonth";
} else {
echo 'There were no dates on or before '.$_POST["month"];
}
Hope that helps. 😃
Jon