Page 1 of 1
how to select current month
Posted: Sun Jul 03, 2011 11:27 am
by vin_akleh
how can i do this?
select * From table_name Where date = currentmonth
when date is a double and i insert it like so date = date("U")
so i want to select the month of date("U") for the current month
Re: how to select current month
Posted: Sun Jul 03, 2011 11:46 am
by califdon
First of all, "date" is a reserved word and should not be used as the name of a field in a table.
http://developer.mimer.com/validator/sq ... -words.tml
The "U" only refers to the format in which to display a datetime value. All datetime data type fields are stored in the same format.
You can make your comparison like this:
Code: Select all
$sql = "SELECT * FROM table_name WHERE month(field_name) = ".date('n')
The SQL function month() returns a value from 1 to 12, as does the PHP function date('n'). You can see why using "date" as a field name is bad.
Notice that this only matches the month, not the year, so if your data contains entries for other years and you are only interested in the current month of the current year, you will need to extend the logic to do that.