MySQL Due Date

2

I have a register where I store the product registration date. However I need to exclude these products after 5 days registered in the database. I tried to use PHP with the following code:

$dataExcluir = mktime(null,null,null,date('m'),date('d') + 5,date('Y'),null);

while($jm = mysqli_fetch_object($sql)){   
    $data = date($jm->DataCadastro,$dataExcluir);
    echo $data."<br>";     
}

But it does not work, that is, it is not 5 days ahead, but I put it in PHP itself:

date('Y-m-d',$dataExcluir);

Would anyone know how to solve this?

    
asked by anonymous 03.06.2015 / 20:43

2 answers

3

You can perform operations on a date using the combination of functions date () with that converts the date to a number, then you can add / subtract them with the day, month, year etc.

echo date('Y-m-d', strtotime('2015-05-01 + 5days'));

One option is to use the DateTime classes and DateInterval . The add () method expects a DateInterval object that in its constructor has the information of what will be added in the case P5D is a period of 5 days.

$dataVencimento = new DateTime('2015-05-01');
$dataVencimento->add(new DateInterval('P5D'));
echo $dataVencimento->format('Y-m-d');

Example -ideon

    
03.06.2015 / 21:12
0

According to the PHP documentation , the first argument of the date () function is the format with the which one you want to display the date, and has nothing to do with doing dates arithmetic. Dates and times in PHP (as well as the Unix C library) are integers, counting the number of seconds since January 1, 1970; Do you want something like

while ($jm = mysqli_fetch_object($sql)) {   
    $data = date('Y-m-d', strtotime($jm->DataCadastro) + 5 * 86400);
    echo $data."<br>";     
}

(You may want to set a constant SEGUNDOS_EM_UM_DIA = 86400; in some part of your code.)

    
03.06.2015 / 20:52