Jump to content

Recommended Posts

I can't get this to work, I need to pull the last 30 days records:

 

:facewall: :facewall:

   
                                     $pastDays = strtotime("-30 days");

                                    $date = date("m/d/y", $pastDays);
             
                                   $sql = "SELECT DISTINCT restaurants.ID, name, address, inDate FROM 
restaurants, inspections WHERE restaurants.name != '' AND 
datediff(curdate(),inspections.inDate)>=$date GROUP BY restaurants.ID 
ORDER BY 'name' ";

Link to comment
https://forums.phpfreaks.com/topic/168285-solved-dates-and-queries/
Share on other sites

What about using this syntax? Straight from mysql manual date and time functions.. says it will fetch the results from last 30 days.. (also from future but you should not probably have future times in there anyway). IN case you do, you can add one AND there to prevent future dates.

SELECT something FROM tbl_name WHERE DATE_SUB('put_date_here', INTERVAL 30 DAY) <= date_column;

SELECT r.ID, r.name, r.address, i.inDate 
  FROM restaurants r, inspections i 
  WHERE r.ID = i.<FIELD WITH RESTAURANT ID>
    AND r.name != ''
    AND DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= i.inDate
  GROUP BY r.ID, r.name, r.address, i.inDate
  ORDER BY r.name

You need to fill in <FIELD WITH RESTAURANT ID> with the column that holds the restaurant id in the inspections table. Then the rest *should* work.

 

edit: with this method you don't need any of the PHP strtotime() code

Nope still can't get it to work tried this:

 

 
                                $pastDays = strtotime("-30 days");
                           $today = getdate();

                           $date = date("y/m/d", $today);
                           
                           

                           $sql = "SELECT DISTINCT restaurants.ID, name, address, inDate FROM 
restaurants, inspections WHERE restaurants.name != '' AND 
DATE_SUB('$today', INTERVAL 30 DAY) <= inspections.inDate GROUP BY restaurants.ID 
ORDER BY 'name' ";

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.