开发者

How can I search for any day in a month?

开发者 https://www.devze.com 2022-12-20 10:43 出处:网络
I\'m doing a search through a database to find articles written within a certain month: between \'1 \'.$month.\' \'.$year and \'31 \'.$month.\' \'.$year

I'm doing a search through a database to find articles written within a certain month:

between '1 '.$month.' '.$year and '31 '.$month.' '.$year

Is this method ok even if some months (like February) only have 28 days? In other words, do I have to dyn开发者_JS百科amically find the number of days in the month, or not?


No, you don't need to know the lengths of the months. Just do this:

WHERE MONTH(yourcolumnname) = $month AND YEAR(yourcolumnname) = $year


Your code looks o.k. in the sense that it will work, but a more elegant approach would be

SELECT * FROM tablename WHERE MONTH(columname) = '1'
                              AND YEAR(columnname) = '2009'


If it's a DATETIME or DATE field, you could also do SELECT * FROM table WHERE date_column LIKE '2010-02-%'; With indexing, this may be faster than the MONTH() and YEAR() technique, which'll have to run a calculation on each row.


Can you search between

between '1 '.$month.' '.$year and '1 '.$month_plus1.' '.$year

?

Have you tried running your sql statement against the data you are trying to match, that might be a good approach to begin with.

0

精彩评论

暂无评论...
验证码 换一张
取 消