Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...