Vulcu Radu Vulcu Radu - 6 months ago 39
PHP Question

Search Between dates YII

We have a date example : 2016-05-10, i am trying to get a query from database using this date.

We have 2 tables : publish_at and end_at

SELECT * FROM `news` WHERE end_at <= '2016-05-11' AND publish_at >= '2016-05-10'


This returns incorrect data as it does not show an event that has publish_at :2016-05-10 and end_at 2016-05-12

SELECT * FROM news WHERE '2016-05-11' BETWEEN publish_at AND end_at


This returns correct item but does not return if date is 10 or 12 only if between

Answer

The first query return correct data since the query reqest all events that publish in the 2016/05/10 or after and ended in the 2016/05/11 or before.

Change the query to

Select * from news where end_at between '2016-05-10' and '2016-05-11' or publish_at betweeb '2016-05-10' and '2016-05-12'
Comments