Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date 1

Status
Not open for further replies.

yebaws

Technical User
Mar 22, 2007
42
GB
Trying to extract rows from a database where the renewal_date
is sooner than 30 days with this code:

Code:
//get todays date 
$today = getdate();
//format date to the same format as "renewal_date" datestamp colummn in database ie YYYY-MM-DD -  "mday" = day of month, "mon" = month, "year" = year 
$todaystamp=$today["year"]."-".$today["mon"]."-".$today["mday"];
echo "Todays date: $todaystamp";

$SQL = " SELECT id, business_name, renewal_date FROM listings WHERE paid = '01' AND $todaystamp - 30 < renewal_date";
$retid = mysql_db_query($db, $SQL, $cid);

if (!$retid) { echo( mysql_error()); }
while ($row = mysql_fetch_array($retid)) {
		$id = $row["id"];
		$business_name = $row["business_name"];
		$email = $row["email"];
		$renewal_date = $row["renewal_date"];
		
echo "Soon due for renewal:";
echo "<br/>";
echo "$id";
}

..but not getting the results I expected. Where am I going wrong?
 
you don't have to construct a variable for today's date -- just use CURRENT_DATE

mysql date arithmetic uses INTERVALs which always require specifying the interval value, i.e. in this case you want DAY
Code:
SELECT id
     , business_name
     , renewal_date 
  FROM listings 
 WHERE paid = '01' 
   AND renewal_date > CURRENT_DATE - 30 [red]DAY[/red]

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Thanks,

Looks more logical than my approach, but the DAY bit returns a syntax error when I write the command just as you've written it?
 
mind you, if I take out the DAY an just use CURRENT_DATE - 30 it seems to work fine...?
 
Thanks - works with INTERVAL, so I'll put it in. But it did seem to work without the DAY and the INTERVAL....
 
yes, i think it defaults to days

however, it's better to be explicit (it's standard SQL, too)

sorry for the confusion/misdirection



r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top