Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get results greater than yesterday

Tags:

mysql

I have a database that stores the date for each entry with the DATETIME format. I need to only retrieve results that are greater than yesterday. I say yesterday because I need the results for the current day and forward.

Currently I have the following.

$yest = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
$yest_date = date('n-j-o', $yest);

SELECT * FROM TBL_NAME WHERE DATE_FORMAT(event_date, \"%c-%d-%Y\") > '".$yest_date."'

That does not give any results even though I know there are events. Any help would be greatly appreciated.

like image 370
jimcone Avatar asked Jun 09 '11 15:06

jimcone


3 Answers

Select * from tbl_name WHERE event_date > DATE_SUB(curdate(), INTERVAL 1 DAY)

This should have it start from the beginning of yesterday rather than 24hours back from the time the query is run.

If you have this run in a cron you should probably verify the timezone of the database vs the server so that you don't run it for two days back intending to run at 12:01 but actually running at 11:01 due to variance.

like image 191
Timwillhack Avatar answered Sep 19 '22 04:09

Timwillhack


SELECT * FROM tbl_name WHERE field_date > now()- interval 1 day;
like image 32
PodTech.io Avatar answered Sep 17 '22 04:09

PodTech.io


WHERE event_date > date(now()) - 1
like image 34
Tomalak Avatar answered Sep 21 '22 04:09

Tomalak