Web Hosting Talk







View Full Version : PHP - MySQL Select Help


HostRefugee-Vince
03-19-2007, 05:25 AM
Hi,

I have a populated database with the date in the following format:

2007-01-28
YEAR-MONTH-DAY

I want to run a SELECT statement that only grabs rows with a date older than or equal to 90 days from today's date.

PLAIN TEXT SQL STATEMENT:
SELECT plan FROM domains WHERE status='1' AND id='$appliestoID' AND dateActivated is older than OR equal to 90 days from today's date.

Would someone be willing to translate that into valid php? The only part I am really having trouble with is the date part.

NastyPasty
03-19-2007, 06:47 AM
Hi, abit rusty with the old mysql date selection methods, but I imagine you can use something like:

SELECT plan FROM domains WHERE status='1' AND id='$appliestoID' AND dateActivated>=DATE_ADD(CURDATE(), INTERVAL -90 DAY)

I'm sure this isn;t 100% correct, but this is along the lines of what you need.

Dave.

jimpoz
03-19-2007, 11:10 AM
Close. Either of the follwing should work:

SELECT plan FROM domains WHERE status='1' AND id='$appliestoID' AND dateActivated<=DATE_SUB(CURDATE(), INTERVAL 90 DAY)
or
SELECT plan FROM domains WHERE status='1' AND id='$appliestoID' AND dateActivated<=CURDATE()- INTERVAL 90 DAY

ak7861
03-20-2007, 02:19 AM
Your field carrying the date should always be timestamped. Then you can easily call time() and subtract 90 x 24 x 60 x 60.

sergeysg
03-20-2007, 05:43 PM
Another option is to use to_days() function:

SELECT plan FROM domains WHERE status='1' AND id='$appliestoID' AND
to_days(now())-to_days(dateActivated)>=90