Reputation: 1481
Im trying to develop a search system with php and Mysql, but how do you search for : example the month of march when the date format in the database is 2012-03-26(year/month/day)
Upvotes: 0
Views: 3627
Reputation: 12973
This is a rather crude example of how you might handle the date in your search form -
<form method="get" action="">
Year:
<select name="year">
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
</select>
Month:
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
</select
<input type="submit" name="search" value="search"/>
</form>
<?php
if (isset($_GET['search'])) {
$year = intval($_GET['year']);
$month = intval($_GET['month']);
$sql = "SELECT *
FROM `table`
WHERE `date` BETWEEN '$year-$month-01' AND LAST_DAY('$year-$month-01')";
}
Upvotes: 0
Reputation: 15686
You can use the mysql MONTH() function like this:
SELECT * FROM table WHERE MONTH(date_column) = 3
to search for March for example, or there is also MONTHNAME() function like this:
SELECT * FROM table WHERE MONTHNAME(date_column) = 'march'
Upvotes: 2
Reputation: 95121
Try Something like
SELECT keyword , description , url FROM searchTable WHERE MONTH(addDate) = '3'
SELECT keyword , description , url FROM searchTable WHERE MONTHNAME(addDate) = 'March'
Upvotes: 0
Reputation: 2101
Depending on the format of the column data, something like this could work: (one of many solutions)
SELECT [whatever]
FROM [wherever]
WHERE `date` < '2012-03-31 23:59:59'
AND `date` > '2012-03-01 00:00:00'
Upvotes: 4