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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.