Calculate expiration date

4

I have a column named START in DATETIME format in MySQL and I would like to check if each row is expired and / or how many days are left to win that row, taking into account that the row expires in 30 days after the START date .

I thought of getting the date and subtract with the current date but I do not know if it has like in PHP, how to do it?

    
asked by anonymous 08.09.2015 / 21:52

2 answers

3
SELECT 
    START, 
    DATE_ADD(START, INTERVAL 30 DAY) AS DATA_VENCIMENTO,      
    DATADIFF(START, DATE_ADD(START, INTERVAL 30 DAY)) AS DIFERENCA
FROM 
    TABELA

In this SELECT you can recover the difference in days between a Data Start and the Expiration Date (30 days later).

And in PHP you can check if the number is negative, if that is why you have already won:

if($res['DIFERENCA'] < 0)
    echo "Vencido!";
else
    echo "Em dia";
    
08.09.2015 / 21:59
2

SELECT start FROM table WHERE DATE_SUB (start, INTERVAL 30 DAY) < now ();

Using the "start" value, it adds 30 real days and takes the records that are GREATER than the date of execution of the script.

EX: expiration date = 2015-09-09 (data start projects + 30 days) and today is 2015-09-08, this line would not be returned.

I hope I have been clear.

    
08.09.2015 / 23:57