Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL date comparison

Tags:

mysql

Dates are stored in DB as such: "2011-05-26 11:00:00" in a datetime field. I'd like to find all rows where the date is greater than the first of this month, ie: 2011-05-01 09:00:00

The date formats in the DB can not be changed. What MySQL function can I use to convert the date in the DB to a format that can handle comparison? I'm guessing the best format for "first of the month" is a unix timestamp?

The time values in the dates are always present but only office hours, so from 09:00 to 17:00.

like image 478
stef Avatar asked May 24 '11 13:05

stef


2 Answers

If it's stored in a DATETIME field, just query on WHERE date > '2011-05-01 09:00:00'.

like image 126
deceze Avatar answered Oct 07 '22 08:10

deceze


$firstDayOfMonth = date( 'Y-m-d H:i:s', mktime(0,0,0,date('n'),1,date('Y'));
$query = "SELECT * FROM `table` WHERE `date` >= '$firstDayOfMonth'";
like image 31
George Cummins Avatar answered Oct 07 '22 06:10

George Cummins