Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get today's / yesterday's data from MySQL database?

Tags:

php

mysql

I would like to retrive a TODAY'S data from the database, but I don't know how to do it. I would actually want to get the data from NOT the past 24 hours, I just want today's data (so based on the actual server time).

I would also like to get data which was yesterday. Can anyone help me how to do it?

Sample code:

"SELECT id FROM folk WHERE time = ???"

Thank you in advance!

like image 781
user1406071 Avatar asked Sep 23 '12 11:09

user1406071


1 Answers

I think you are looking for this:

"SELECT id FROM folk WHERE DATE(time) = CURDATE()"

time must be a field in you table that holds a reference to the row.

update

To get yesterdays additions:

"SELECT id FROM folk WHERE DATE(time) = CURDATE() - 1"

update 2

To get all additions this month:

"SELECT id FROM folk 
WHERE MONTH(time) = MONTH(NOW()) AND YEAR(time) = YEAR(NOW())"

reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

like image 191
JvdBerg Avatar answered Oct 12 '22 12:10

JvdBerg