Hello.

Suppose a person chose that his news will be visible for 3 months.
When adding news, the start date of the show is written in the format. 0000-00-00 00:00:00
The number of months is written in integers.

I can not understand how in the request to the current date I add a certain number of months to make up a condition. The main thing is that not just the days be taken into account, but also the time when the news was added.

I would be grateful for the help.

  • 6
    Simply specify + INTERVAL N MONTH : SELECT UTC_TIMESTAMP () + INTERVAL 3 MONTH Instead of MONTH can be DAY , WEEK etc - BOPOH
  • 2
    ... or calculate the end date of visibility immediately and have it written to the database. - Yura Ivanov
  • @BOPOH, thank you very much.! @Yura Ivanov, yes, probably it will be better? - drop_off

1 answer 1

Reply from comments:

In order to add an interval to the calendar value, you can use the INTERVAL keyword, for example, if the date is stored in the created_at field, you can get the date after 3 months using the following query

 SELECT created_at + INTERVAL 3 MONTH AS finished_at FROM news 

You can retrieve news that is relevant for display using the request

 SELECT * FROM news WHERE NOW() >= created_at AND NOW() <= created_at + INTERVAL 3 MONTH