开发者

Select Before The Date

开发者 https://www.devze.com 2023-02-05 18:38 出处:网络
I have a date, say its called $date. I want a a mysql_query to search a select number of weeks,days or even months before my $dat开发者_JAVA百科e. Is this possible? My explanation is not the greatest,

I have a date, say its called $date. I want a a mysql_query to search a select number of weeks,days or even months before my $dat开发者_JAVA百科e. Is this possible? My explanation is not the greatest, but I do need a answer for this and do not know how to properly question it.


You could use mysql interval function?

"select * from table where `date` BETWEEN DATE_SUB(".$date.",INTERVAL 15 DAY ) AND CURDATE( )

That'll return the records from the last 15 days, you could use = insted of between if you want the records exactly 15 days old, or modify it for days, months, etc.

edit: if your working with php's time() remeber to use FROM_UNIXTIME($phpdate) inside your query.


i have a solution for this in SQL, Take it, if it would helps you

Day($date) gives you the date in the vaariable Month($date) gives you the Month in the vaariable Year($date) gives you the year in the vaariable

using simple where conditions, now you can search for a particulars


You can use the DATE_ADD and DATE_SUB functions to modify a date, and mysql understands a BETWEEN clause using dates. However, you can also use the TIMESTAMPDIFF function like so:

"SELECT foo FROM table WHERE TIMESTAMPDIFF(DAY, dateField, '$date') < '$desired_days'"
0

精彩评论

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