Jump to content

Query From the Database where date is older than 2 weeks


madjack87

Recommended Posts

In the database I am storing the date with CURDATE

 

What I need to do is query the database to show any records that are more than 14 days older then todays date.

 

$result = mysql_query("SELECT * FROM customers WHERE stage = 0, TO_DAYS(CURDATE()) - TO_DAYS(date)>14 ORDER BY date");

 

I know the above code is not correct I just wanted to show everyone what I have been trying.

 

Can I use DATE SUB?

 

Any help would be appreciated.

 

thanks

I tested that query locally, and the syntax is correct. I assume you edited it to match your table structure, right? Echo the query string, along with mysql_error(), and post those please.

 

$query = "SELECT `field` FROM `table` WHERE `date` < DATE_SUB( CURDATE(), INTERVAL 14 DAY )";
if( !result = mysql_query($query) ) {
     echo "<br>Query : $query<br>Failed with error: " . mysql_error() . '<br>';
}

 

EDIT: I see where I originally left a closing `backtick` off of the table name. Did you catch that and add it?

Here is my Error

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' < DATE_SUB( CURDATE(), INTERVAL 14 DAY ) ORDER BY date' at line 1

 

here is my code

$result = mysql_query("SELECT * FROM customers WHERE date' < DATE_SUB( CURDATE(), INTERVAL 14 DAY ) ORDER BY date") or die ('Error: '.mysql_error ());

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.