1

I have a tabel in MYSQL database that has 2 columns.

1- start_date
2- end_date

the above columns both hold DATETIME like so:

start_date                  end_date

2019-10-03 13:30:00     2020-10-03 23:30:00 

I need to select from the MYSQL table ONLY if the current date is between the start_date and end_date.

So i tried this MYSQL Query:

SELECT * FROM table WHERE `end_date` >= CURDATE() AND `start_date` <= CURDATE()

but this doesn't work and doesn't return anything from the database even thogh the the current date is between the start_date and end_date.

Could someone please advice on this?

2
  • No date can be greater than today >= CURDATE() and less than today <= CURDATE() at the same time. Commented Oct 3, 2019 at 12:33
  • 1
    @JayBlanchard, when it comes to dates, my head starts making strange noises! don't know why. Commented Oct 3, 2019 at 12:34

3 Answers 3

1

Q: I need to select from the MYSQL table ONLY if the current date is between the start_date and end_date.

Try this.

SELECT * FROM `table` WHERE CURDATE() between start_date and end_date

Convert it with date before because you're using their timestamp.

SELECT * FROM `table` WHERE CURDATE() between date(start_date) and date(end_date)

Or

SELECT * FROM `table` WHERE NOW() between start_date and end_date
Sign up to request clarification or add additional context in comments.

9 Comments

Jeeez. that easy?
Yep, it's just that easy.
But that doesn't return anything either! the date i have are start_date 2019-10-03 13:30:00 AND end_date 2020-10-03 23:30:00. I would expect that to be returned since the currect date is between those two dates!
Try make date(end_date) and date(start_date).
Note that the second query cannot use an index :-( (but the third option can) :-)
|
1

Using SQL date function in query

SELECT * FROM user WHERE date(end_date) >= CURDATE() AND date(start_date) <= CURDATE()

Comments

0

try this

SELECT * FROM table 
WHERE start_date >= '2019-10-03 13:30:00' 
AND end_date <= '2020-10-03 23:30:00'

1 Comment

the dates are stored in the MYSQL. cant hardcode them in my php query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.