Jump to content

Add 30 day to MySQL query


Go to solution Solved by objnoob,

Recommended Posts

In the/ MySQL database date field stored the date in UK format (dd/mm/yyyy).

eg : 01/08/2013

 

In the query I would like to add 30 days to the data field and match it to the current date.

If it matches, show results.

 

For example, if the data field value 01/08/2013, when I query I will add 30 days in the php code then will become  31/08/2013. if the current date was 31/08/2013 this will match.

 

I am a bit confused on how I can do this, can you please give advice on how to do this

Link to comment
https://forums.phpfreaks.com/topic/281785-add-30-day-to-mysql-query/
Share on other sites

That date format is totally useless for databases. You can't compare date ranges, you can't sort and you can't make use of the dozens of dat/time functions without converting it first to the correcty YYYY-MM-DD format.

 

Use STR_TO_DATE() function to convert to DATE format then you can add INTERVAL 30 DAY to the result.

  • Solution

You should do this............

 

ALTER TABLE your_table ADD COLUMN theDate DATE default null;      -- adds a column to the table that is of date datatype

UPDATE your_table SET theDate =  STR_TO_DATE(theUKDate);          -- updates the table setting the new column to the correct date in the correct format

ALTER TABLE your_table DROP COLUMN theUKDate;                           -- drops your column that is storing the date in the UK format which is useless

 

Now you're storing the date properly in the standard proper format.

When you need to query and wish to return the date in UK format you can use DATE_FORMAT function....

 

SELECT DATE_FORMAT(theDate, '%d/%m/%Y') as 'UK Date' FROM your_table;

... anyhow, my main intention was to offer an efficient alternative solution.

 

It's only efficient if it works, and date arithmetic will not work with d/m/y date formats

 

 

is it possible to convert it within the query

 

Yes.

... WHERE CURDATE() = STR_TO_DATE(ukDate, '%d/%m/%Y') + INTERVAL 30 DAY
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.