I’m trying to search for all appointments between to dates in an mysql database. The date is in this format.
2012-07-26
Here is the code I’m using to select the relevant appointments from drop down selection boxes on another page.
AND SUBSTRING(startDate, 6, 2) >= $month
AND SUBSTRING(startDate, 6, 2) <= $month2
AND SUBSTRING(startDate, 9, 2) <= $day
AND SUBSTRING(startDate, 9, 2) <= $day2
The problem I have is that if I select a day like 02, then the results will only be days earlier than 02 on any month. Whereas I need something more like;
SUBSTRING(startDate, 6, 5) <= '$month2'-'$day2'
I’m just not sure how to phrase it correctly.
You should MySQL’s date functions instead of
SUBSTRINGassuming your column is ofDATEorDATETIMEtype.Relevant date functions:
DAY()MONTH()New code:
You may also be able to use the
BETWEENoperator.Documentation