Link to home
Start Free TrialLog in
Avatar of grantunwin
grantunwin

asked on

MySQL Problem

In a solution provided in a previous question I was given this:

$rd = $_REQUEST['start_date'];
$sql = "SELECT * FROM `listings` WHERE `date` >= date_add(str_to_date('$rd', '%d/%M/%Y'), interval -3 day) and `date` < date_add(str_to_date('$rd', '%d/%M/%Y'), interval 4 day) ";

I need to know if this would work:


$rd = $_REQUEST['start_date'];
$sql = "SELECT * FROM `listings` WHERE str_to_date(`date`, '%d/%M/%Y') >= date_add(str_to_date('$rd', '%d/%M/%Y'), interval -3 day) and str_to_date(`date`, '%d/%M/%Y') < date_add(str_to_date('$rd', '%d/%M/%Y'), interval 4 day) ";

ASKER CERTIFIED SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Using any function on the `date` column also causes an index on that column to be unusable.  Consider making a date range that will cover the date+time involved.
If `date` contains time data and you want to include the 7th day, then try this

$rd = $_REQUEST['start_date'];
$sql = "SELECT * FROM `listings` WHERE `date` >= date_add(str_to_date('$rd', '%d/%M/%Y'), interval -3 day) and `date` < date_add(str_to_date('$rd', '%d/%M/%Y'), interval 5 day) ";

< ".. interval 5 days "

will include any hour on the day "$rd +4 days"