Canman2005 Posted September 3, 2009 Share Posted September 3, 2009 Hi all 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. Does that make sense? Can anyone help? Thankd very much Ed Quote Link to comment Share on other sites More sharing options...
waynew Posted September 3, 2009 Share Posted September 3, 2009 So... you just wanto grab everything that hasn't expired? Or does it specifically have to be 5 months or less left? Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted September 3, 2009 Author Share Posted September 3, 2009 yes please, specifically have to be 5 months or less left Quote Link to comment Share on other sites More sharing options...
mystery_man Posted September 3, 2009 Share Posted September 3, 2009 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! Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted September 3, 2009 Author Share Posted September 3, 2009 Top man, thanks dude Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.