DogPooOnYourShoe
DogPooOnYourShoe

Reputation: 149

Select Before The Date

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 $date. 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.

Upvotes: 1

Views: 708

Answers (3)

Tesserex
Tesserex

Reputation: 17314

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'"

Upvotes: 0

Karthick
Karthick

Reputation: 1

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

Upvotes: 0

jlasarte
jlasarte

Reputation: 643

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.

Upvotes: 2

Related Questions