Re: MySQL query
| From: | CPT John W. Holmes | Date: | Mon, 12 Jan 2004 20:36:34 +0000 |
| Subject: | Re: MySQL query | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-174368@lists.php.net to get a copy of this message | ||
From: "Ashley M. Kirchner" <ashley@pcraft.com>
> I need to query for record within a certain date stamp. The
> datetime field contains the createdon information that i need and is in
> the following format: 2004-01-11 21:40:50
>
> What I'd like to do is search for records that are between
> 2004-01-09 and 2004-01-04. The time is to be ignored, just the date is
> what I need to search by. Is there a way to do this within an sql
> query, or do I have to do some PHP processing of the datetime field to
> get what I need?
You'll want to do it in the query, although PHP could help a little.
Even though you want the time to be ignored, the easiest way to do this is
to actually search from 2004-01-09 00:00:00 through 2004-01-04 23:59:59.
Since the "00:00:00" and "23:59:59" are "constants" you can just have
PHP
add them to the date string or hard code them into your query. Given that
format, the query would be as simple as:
SELECT * FROM table WHERE datetimefield BETWEEN $start AND $end
Where $start is "2004-01-09 00:00:00" and $end is "2004-01-04 23:59:59"
If you don't want to do that processing, then you'll need to use another
method.
SELECT * FROM table WHERE TO_DAYS(datetimefield) BETWEEN TO_DAYS($start) AND
TO_DAYS($end)
Where $start is "2004-01-09" and $end is "2004-01-04".
There are other methods, too, but the end results is you need to take your
"datetimefield" and get it into the same format as $start and $end.
If you're using MySQL 4.1, there is a DATE() function (I think) that'll
allow you to do this:
SELECT * FROM table WHERE DATE(datetimefield) BETWEEN $start AND $end
Where $start is "2004-01-09" and $end is "2004-01-04".
Hope that helps.
---John Holmes...