I've got two columns (both datetime) startDate and endDate in an events table.
I am retrieving the current day using the the date()
function of php.
This results in for example 2013-03-12.
Now there are three possibilities of events combined with dates that occur today:
Now I'd like to break these all into separate queries as I'm not used to work with dates. I started with the first query, but I am already failing on that one. I've tried the following:
SELECT * FROM events WHERE (startDate= '2013-03-12' AND endDate= '2013-03-12')
aswell as:
SELECT * FROM events WHERE NOT (startDate < '2013-03-12' OR endDate > '2013-03-12')
I've tried to use DATE()
aswell and to format dates like '2013-03-12%'.
I don't know why it doesn't work while i am sure there is at least 1 event that is taking place on the 12th. Any help is appreciated.
To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days.
How to Select rows from a range of dates with MySQL query command. If you need to select rows from a MySQL database' table in a date range, you need to use a command like this: SELECT * FROM table WHERE date_column >= '2014-01-01' AND date_column <= '2015-01-01';
To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference.
You can use str_to_date to convert a date string to MySQL's internal date format for inserting.
Try using the MySQL's DATE()
function to trim the date columns to the just the date parts:
SELECT *
FROM events
WHERE (DATE(startDate) = '2013-03-12' AND DATE(endDate)= '2013-03-12')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With