Solved

MySQL Problem

Posted on 2010-09-11
2
390 Views
Last Modified: 2012-05-10
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) ";

0
Comment
Question by:grantunwin
  • 2
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33653363
Only if the column `date`contains not date data, but VARCHAR data in d/M/Y format.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33653370
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"
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now