Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql date interval

I am trying to select rows which are within a certain time period:

$sql = mysql_query(" SELECT * FROM table WHERE ... AND 
       date BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
       OR date ISNULL");

The format of the dates saved in this column are mm/dd/yyyy

a. how do I select the time interval? currently it is selecting all the rows, not just those within this date range.

b. Am I using the corret syntax to make sure we include all null options as well?

Update.

Following the answers and comments I changed the column type to "Date" (it was varchar), and have also changed the current type of the existing entries to the correct date format.

I now have 2 questions: a. This is still not working... What else might be the problem? b. I want to change in PHP where the entry is saved. How ahould I go about this? Is the following correct:

 $new_date = mysql_query("STR_TO_DATE($old_date_string,'%m/%d/%Y')");

Solution.

First, I changed the column type to DATE (from VARCHAR).

Second, I moved and converted all the existing entries from their existing column and type (str) to new type (date) and new column.

Third, I added parentheses to the "date" section.

Fourth, I changed ISNULL to IS NULL - addin a space, which changes it from a function to a statement.

Fifth, I am now updating my script so that in the future dates will be saved to the new column in the new type, converting them to the correct fprmat before saving them to the database.

Thankyou @nnicholas and everybody.

like image 582
Lucy Weatherford Avatar asked May 03 '26 20:05

Lucy Weatherford


2 Answers

If you are really storing your dates as strings I suggest you update your table with something like the following -

-- add a new column of type DATE
ALTER TABLE `table` ADD COLUMN `date_new` DATE AFTER `date`;
-- populate the new column from the old one
UPDATE `table` SET `date_new` = STR_TO_DATE(`date`,'%m/%d/%Y');
-- drop the old column
ALTER TABLE `table` DROP COLUMN `date`;
-- rename the new column
ALTER TABLE `table` CHANGE `date_new` `date` DATE;

After making these changes handling dates will be much easier for you. When inserting the dates that you are receiving you simply use STR_TO_DATE('new_value','%m/%d/%Y') to convert the date during the insert -

mysql_query("INSERT INTO `table` (field1, field2, `date`) 
               VALUES('value1', 'value2', STR_TO_DATE('$old_date_string','%m/%d/%Y')");

This assumes that $old_date_string has already been sanitised.

With your table structure updated the standard date arithmetic functions will work as intended.

$sql = mysql_query("SELECT *
                    FROM table
                    WHERE ...
                    AND (`date` BETWEEN ('$date' - INTERVAL 2 YEAR) AND ('$date' + INTERVAL 2 YEAR) OR `date` IS NULL)");

$date must be in Y-m-d format.

like image 104
nnichols Avatar answered May 05 '26 09:05

nnichols


You can't do date comparisons on it if it's not stored in the database as a date.

You could do something very ugly casting it to a date with STR_TO_DATE() and then comparing but I'd suggest reworking your table structure to store this date as a DATE object.

SELECT * FROM table WHERE ... AND 
    STR_TO_DATE(date, '%m/%d/%Y') BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
    OR date ISNULL

This assumes that $date is in the correct MySQL DATE format of 2012-03-14 (for 14th March 2012)

like image 43
James C Avatar answered May 05 '26 10:05

James C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!