how to select current month

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
vin_akleh
Forum Commoner
Posts: 53
Joined: Sat Feb 14, 2009 10:26 am

how to select current month

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to select current month

Post 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.
Post Reply