jingleboy99
jingleboy99

Reputation: 725

Mysql query: retrieve current date query

In mysql database i have this column called:

Name: Date

Type: datetime

I have few values in that column:

2009-01-05 01:23:35

2009-03-08 11:58:11

2009-07-06 10:09:03

How do I retrieve current date? I am using php.

in php:

<?php $today = date('Y-m-d');?>

How to write a mysql query to retrieve all today date data?

Should i change the column type to "date", then insert values like "2009-07-06" only, no time values???

Upvotes: 2

Views: 33031

Answers (2)

Waqar Alamgir
Waqar Alamgir

Reputation: 9968

For date time it is not usefull, instead I try this and working...

Today's Visitors!

sql > select user_id from users where last_visit like concat('%' , CURDATE() , '%');

// last_visit coloumn of type 'datetime'

Upvotes: 1

Paolo Bergantino
Paolo Bergantino

Reputation: 488404

You don't need to use PHP, MySQL has a function to get the current date:

SELECT field FROM table WHERE DATE(column) = CURDATE()

Documentation: CURDATE, DATE.

If your column is only ever going to need the date part and never the time, you should change your column type to DATE. If you insist on doing it through PHP, it is the same thing, really:

$today = date('Y-m-d');
$query = mysql_query("
    SELECT field FROM table WHERE DATE(column) = '$today'
");

Upvotes: 15

Related Questions