Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting entries by date - >= NOW(), MySQL

Tags:

date

sql

mysql

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

like image 408
NightMICU Avatar asked Jun 25 '10 00:06

NightMICU


People also ask

What is now () in MySQL?

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).

Where date is greater than current date MySQL?

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.

How do I select a specific date in MySQL?

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.

How do I query between two dates using MySQL?

select *from yourTableName where yourColumnName between 'yourStartingDate' and curdate().


2 Answers

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()"); 
like image 130
Sarfraz Avatar answered Sep 21 '22 23:09

Sarfraz


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() 
like image 24
OMG Ponies Avatar answered Sep 23 '22 23:09

OMG Ponies