R
R
Rodion Yurchenko2015-07-02 09:55:30
PHP
Rodion Yurchenko, 2015-07-02 09:55:30

How to select a record from the database by time interval?

Good day everyone!
Recently, a new project was given - and faced with the following task
. The user of the site can create a thread of the event, and at the same time indicate the date and time of its start, as well as the date and time of its end.
Well, then the server should show
these events, but - those events, the start date and time of which has not yet come - do not need to be shown
That's actually the question - how best to implement it in the database,
I thought like this:
id
event_text
date_start (DATETIME)
date_end (DATETIME)
and then compare in sql query -

SELECT * FROM table WHERE date_start<CURDATE() AND date_end>CURDATE()

but even does not want to work ...
then I found that in addition to CURDATE there is CURTIME
, it turns out I need to add 2 more fields to the table (time_start, time_end) to write down the time in them, and compare already by 4 fields at once ....
I have a feeling that I'm making a bike....
Is there any way to make it simpler?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WebDev, 2015-07-02
@aassdds

You think everything is correct, and implement it, only the type of the field is not DATETIME, but DATE, then everything will work in your example, but this is only if you compare by date. Or use DATETIME and NOW() to both date and time

S
Sergey Ivanov, 2015-07-02
@Writerim

SELECT * FROM start < NOW() AND stop > NOW()
can be compared like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question