Validate a date range within MySQL query
Posted
by
fishcracker
on Stack Overflow
See other posts from Stack Overflow
or by fishcracker
Published on 2012-11-26T10:55:33Z
Indexed on
2012/11/26
11:03 UTC
Read the original article
Hit count: 213
(This question may seem easy or kind of noobish, by that I pardon my ignorance.)
I used PDO query to use SELECT
then fetch some values, it comes to a point that I need to fetch only some entries that within its start date and end date.
My database
+----------+-----------------+----------------------+--------------------+
| id (INT) | title (VARCHAR) | start_date (VARCHAR) | end_date (VARCHAR) |
+----------+-----------------+----------------------+--------------------+
| 1 | buddy | 2012-11-26 | 2012-11-30 |
| 2 | metro | 2012-12-05 | 2012-12-20 |
| 3 | justin | 2012-11-28 | 2012-12-01 |
+----------+-----------------+----------------------+--------------------+
My query is as follows:
$query = "SELECT title, start_date, end_date FROM debts WHERE start_date >= CURDATE() AND end_date >= CURDATE()";
What I want to achieve is whenever the start_date
is today or greater but not exceeding the end_date
it will be valid. This will return the row for id 1
, however if I change the start_date
to 2012-11-25
, it will fail due to the first condition on AND
. I'm really confuse on this since I am new to this, is there any built-in function to handle this kind of situation?
© Stack Overflow or respective owner