Mysql due date

Asked

Viewed 492 times

2

I have a register where I store the registration date of the products. However I need to delete these products after 5 days registered in the database. I tried to use the 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>";     
}

However it does not work, IE, It is not advanced the 5 days, but I put in PHP itself:

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

Someone would know how to fix this?

  • 2

    To discuss matters related to the site visit [meta]. On the subject remove greetings, you can go straight to that link and see that it was the decision of the majority: Greetings and Thanks. You can give your opinion there about why you think the greetings should be kept and listen to what the community has to say. PS: I’m not being sarcastic, you can even give your opinion.

  • Right Math. I’m not against correcting words, codes, etc, but I think a greeting and a thank you should have. I will go to the link and give my opinion. Thank you!

2 answers

3


Can perform operations at a date using the combination of functions date() with strtotime() which converts the date to a number, then you can add/subtract them with the day, month, year modifiers etc.

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

One option is to use the classes Datetime and Dateinterval. The method add() expects a Dateinterval object that in its constructor has the information of what will be added, in 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

  • Perfect rray. It worked!!... Thank you!

0

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

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

(Maybe you want to put a constant SEGUNDOS_EM_UM_DIA = 86400; somewhere in your code.)

  • Got it. the $Jm->Dataregistration variable is coming in mysql DATE() format and when I put your code, it is returning me: 1970-01-05.

  • You have how to print the $jm->DataCadastro? It seems to be coming in string format; a strtotime must do the service of converting the string to an integer.

  • $Jm->Dataregistration returns 2015-06-02. It’s coming from the database date()

Browser other questions tagged

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