Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
164 views
Welcome To Ask or Share your Answers For Others

1 Answer

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'
");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...