I have a column in my mySQL DB that has some rows. One of this row is a DATE, like this: 2012-02-01
What I want to achieve is to do a SELECT with PHP based only on the year and month.
The logic of the SELECT will be the following:
$q="SELECT * FROM projects WHERE Date="SELECT HERE THE SPECIFIC YEAR AND MONTH"";
The specific month and year will be be passed from a $_POST
variable, like this $_POST['period']="2012-02";
How can I do it?
To select all entries from a particular month in MySQL, use the monthname() or month() function.
If you need to select rows from a MySQL database' table in a date range, you need to use a command like this: SELECT * FROM table WHERE date_column >= '2014-01-01' AND date_column <= '2015-01-01';
If you use SQL Server, you can use the DAY() or DATEPART() function instead to extract the day of the month from a date. Besides providing the EXTRACT() function, MySQL supports the DAY() function to return the day of the month from a date.
SELECT * FROM projects WHERE YEAR(Date) = 2011 AND MONTH(Date) = 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With