Subtract a time value (interval) from a date

We can use the following query to get the date difference from a date or even we can use to compare to get the desired result.
SELECT DATE_SUB(CURDATE(),INTERVAL 21 DAY)
It gives the 21 days before date from today date.

Or even we can use it on comparision after WHERE statement as following:

AND DATE_SUB(CURDATE(),INTERVAL 21 DAY) <= FROM_UNIXTIME(date_field,’%Y-%m-%d %h:%i:%s’)

It will display the 21 days ago rows if the date_field is numeric value (FROM_UNIXTIME is used to convert into standard unix time).

If date_field is already in %Y-%m-%d format, then we can use as

AND DATE_SUB(CURDATE(),INTERVAL 21 DAY) <= date_field

This type of query is used in displaying the results for 1 week result or 2 weeks or 3 weeks or 1 year etc.

Comments

comments

8 Comment

  1. Great site. A lot of useful information here. I’m sending it to some friends!

  2. Thanks for an idea, you sparked at thought from a angle I hadn’t given thoguht to yet. Now lets see if I can do something with it.

  3. My cousin recommended this blog and she was totally right keep up the fantastic work!

  4. Hi, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of spam feedback? If so how do you prevent it, any plugin or anything you can recommend? I get so much lately it’s driving me mad so any support is very much appreciated.
    My site is on Depression treatment.

    1. vinay says: Reply

      Hi,
      You can use akismet plugin to prevent spam feedback .

  5. music says: Reply

    Hi there, I found your blog via Google while searching for a related topic, your site came up, it looks good.

    1. vinay says: Reply

      Thanks .

  6. Hello there, You have done an incredible job. I will certainly digg it and personally suggest to my friends.

Leave a Reply