Skip to content
Advertisement

How to calculate if the difference from current date and another date is equal or minus of a number of days

I use this query to calculate if the current date minus another date is minus or equal an amount of days. Unfortunately this query only works if the calculation of days is inside the same month. Let’s take for example these datas:

current_date is: 30/08/2019 and another day is: 27/08/2019 minus or equal 3, the query works but if the current date is, for example, 01/09/2019 it doesn’t work as it returns an empty list.

This is the query I use:

SELECT * FROM condominio WHERE (CURRENT_DATE - condominio.revisione) <= 5

The date are store inside the column in this format: “yyyy-mm-dd” so I think it is correct. I need to put some other data togheter with current_date to have the right values returned from the query?.

Advertisement

Answer

This is the right code for my goal:

SELECT * FROM condominio WHERE TIMESTAMPDIFF(DAY,NOW(), DATE_ADD(revisione, INTERVAL 1 YEAR)) <= 30;

Hope it can help someone else now or in the future 🙂

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement