Jump to content

Recommended Posts

:facewall: I am trying to find records within the last 30 days, this isn't working how would you do it?

 

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

                                    $sql = "SELECT DISTINCT restaurants.ID, name, address, inDate FROM restaurants, inspections WHERE restaurants.name != '' AND inspections.inDate <> $pastDays GROUP BY restaurants.ID ORDER BY 'name' ";

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

What type of field is inDate?  If it's a DateTime, then you would do something like this:

 

$pastDays = date("Y-m-d H:i:s", strtotime("-30 days"));

$sql = "SELECT DISTINCT restaurants.ID, name, address, inDate FROM restaurants, inspections WHERE restaurants.name != '' AND inspections.inDate > '" . $pastDays . "' GROUP BY restaurants.ID ORDER BY 'name' ";

Link to comment
https://forums.phpfreaks.com/topic/167676-solved-dates-and-php/#findComment-884288
Share on other sites

This should work,

 

<?php
$pastDays = time() - (60*60*24*30);

$sql = "SELECT DISTINCT `restaurants`.`ID`, `name`, `address`, `inDate` 
   FROM `restaurants`, `inspections` 
   WHERE `restaurants`.`name` <> '' 
   AND `inspections`.`inDate` > $pastDays 
   GROUP BY `restaurants`.`ID`
   ORDER BY `name`";

 

Is `inspections`.`inDate` a unix timestamp?

Link to comment
https://forums.phpfreaks.com/topic/167676-solved-dates-and-php/#findComment-884291
Share on other sites

ah, I think that's where I screwed up...the field `inDate` is a Varchar and all the dates are 00/00/00 like that because of where the data is being pulled from, so I guess I would have to find out what NOW() is, format that to 00/00/00 formatting and then compare it for a 30 day span...right? is that possible?

Link to comment
https://forums.phpfreaks.com/topic/167676-solved-dates-and-php/#findComment-884877
Share on other sites

when dealing with MySQL you should use one of the DATE types. If you feel overwhelmingly compelled to NOT use them, you should at least use a unix timestamp in the varchar or int field. with dates formatted as MM/DD/YY you have to convert them each time you want to use them, which is a resource waste

Link to comment
https://forums.phpfreaks.com/topic/167676-solved-dates-and-php/#findComment-884888
Share on other sites

No matter what format you receive the data in, you should convert it to a DATE data type when you insert it into your database.

 

DATE data types use less storage, can be directly sorted and compared in less-than/greater-than comparisons, result in the fastest and simplest queries, and can use the many built-in mysql date/time functions which would make a query to do what you want directly possible.

Link to comment
https://forums.phpfreaks.com/topic/167676-solved-dates-and-php/#findComment-885119
Share on other sites

No matter what format you receive the data in, you should convert it to a DATE data type when you insert it into your database.

 

DATE data types use less storage, can be directly sorted and compared in less-than/greater-than comparisons, result in the fastest and simplest queries, and can use the many built-in mysql date/time functions which would make a query to do what you want directly possible.

 

I definitely agree. Always try to choose relevant data types for your tables before inserting data. That will make your life easier and you will have complete freedom to both search the database and easily format everything in the html output.

 

You will save a LOT of time if you design your database structure carefully, both types, relations and indexes.

Link to comment
https://forums.phpfreaks.com/topic/167676-solved-dates-and-php/#findComment-885128
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.