Jump to content

QUERY 5 months remaning


Canman2005

Recommended Posts

I have a field called `expiry_date` and I want to run a QUERY that grabs all records where there is 5 or less months left until the `expiry_date` has been reached.

 

Hi Ed

 

Assuming that your 'expiry_date' field is of type DATE in the database:

 

1. Get the current date in the script in a SQL-acceptable format in $this_var

 

2. Add 5 months to $this_var

 

3. SQL logic: select all records where the date is SMALLER than $this_var (within 5 months of the given date)

 

 

Now for the code:

<?php 

//You can use this function to get the date
function get_my_date(){

//get the current date plus five months
$date = strtotime("+ 5 months", time());

//format the time for SQL (at least MySQL is happy with this)
$time = strftime("%Y-%m-%d %H:%M:%S",$date);

return $time;
//looks like "2010-02-03 14:30:24" if today is "2009-09-03 14:30:24"
}

//using the function, get the time:
$date = get_my_date();

//here is your SQL
$sql = "SELECT * FROM <tablename> WHERE expiry_date < '{$date}'";

//the rest of your script
...

?>

 

I Hope this is helpfull!

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.