I have a Calendar of Events table and I would like to select events with dates equal to or greater than today. When I use the following SELECT statement, it only retrieves events in the future (> NOW()):
<?php $db->query("SELECT * FROM events WHERE event_date >= NOW()"); ?>
How would I go about selecting all events that are either today or in the future?
Thank you
MySQL NOW() Function The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a 'YYYY-MM-DD hh:mm: ss.
You can use DATE() from MySQL to select records with a particular date. The syntax is as follows. SELECT *from yourTableName WHERE DATE(yourDateColumnName)='anyDate'; To understand the above syntax, let us first create a table.
select *from yourTableName where yourColumnName between 'yourStartingDate' and curdate().
You are looking for CURDATE()
:
$db->query("SELECT * FROM events WHERE event_date >= CURDATE()");
Or:
$db->query("SELECT * FROM events WHERE event_date >= CURRENT_DATE()");
The reason that this query:
SELECT * FROM events WHERE event_date >= NOW()
...returns records from the future, is because NOW() includes the time as well as the date. And that time portion indicates the time at which the [function or triggering] statement began to execute. So that means the start of the day in a DATETIME data type looks like: 2010-06-24 00:00:00
(YYYY-MM-DD HH:MM:SS, to microsecond precision), which NOW() would show something like 2010-06-24 14:24:31
...
Here are your options:
SELECT * FROM events WHERE event_date >= DATE(NOW()) SELECT * FROM events WHERE event_date >= DATE(CURRENT_TIMESTAMP) SELECT * FROM events WHERE event_date >= CURRENT_DATE() SELECT * FROM events WHERE event_date >= CURRDATE()
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