Calculate maturity date

Asked

Viewed 961 times

4

I have a column called 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 Row expires 30 days after the START date.

I thought to take the date and subtract with the current date but I do not know if there is how in PHP, how to do it?

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 that SELECT you can recover the difference of days between a Data Start and the Due Date (30 days later).

And in PHP you can check if the number is negative, if it is because you already won:

if($res['DIFERENCA'] < 0)
    echo "Vencido!";
else
    echo "Em dia";
  • 1

    I fixed that line: DATEDIFF(START, NOW()) THE DIFFERENCES and worked perfectly

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 LONGER than the date of the script execution.

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

I hope I was clear.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.