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-0
$firstDayOfMonth = date( 'Y-m-d H:i:s', mktime(0,0,0,date('n'),1,date('Y'));
$query = "SELECT * FROM `table` WHERE `date` >= '$firstDayOfMonth'";
Compare to the datetime directly:
WHERE date_field >= '2011-05-01 00:00:00'
Something like this perhaps?
$oDateTime = new DateTime();
$sQuery = "SELECT * FROM table WHERE date >= ".$oDateTime->format("Y-m")."-01 09:00:00";
You can use TIMESTAMPDIFF function in MySQL to compare time,
FROM_UNIXTIME function to format Unix timestamp as date.
More mysql data and time functions and examples are available in MySQL Reference Manual date and time function .
DATETIME
fields can filtered just like integer fields, example:
SELECT * FROM `table` WHERE `date` > '2011-05-01 09:00:00'
In case you really want to convert to Unix timestamps, have a look at the UNIX_TIMESTAMP function.
I think that you can solve it, something like this:
firstly, create first day of month in mysql format in php
$firsDay = date('Y-m-d 00:00:00', strtotime(date('Y-m').'-01 00:00:00'));
and then, use it in the query
select * from something where date >= $firsDay