views:

1649

answers:

1

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???

+5  A: 

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'
");
Paolo Bergantino
This is my last option. Cuz my server timezone is different from my country. I store the date value in my local timezone...
jingleboy99
I can see the similarity, but i just afraid, just in case, timezone conflict. So, i must change my column to "date" first right? Thank you very much for the quick answer.
jingleboy99
If you change your column to DATE then you can remove the DATE() function call around the column. That function is used to extract the DATE from a DATETIME column for the comparison. If you're only ever going to need the DATE then you should probably change your column type.
Paolo Bergantino
Then you can set the timezone per connection, see http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.htmle.g. set timezone="+10:00"; select now();
nos
@Paolo: Does it matter if i dun need the time, but i still store the date+time value in to database? Can you suggest few solutions, cuz I already have 4000 users subscribed. All stored in datetime values. But I only need the date value only.
jingleboy99
Doing DATE(column) is slower than just column, so you probably should switch it over.
Paolo Bergantino