how to use date interval to count days?

Asked

Viewed 358 times

1

how can I use the date interval to count when registering a product, for example I register the product on the 8th I want that in 15 days this product will no longer be a new product or when it is the 23rd this product is no longer new how can I do it using the date interval?

code :

 $criacao_produto = new DateTime($data);
 $dias = $criacao_produto->diff(new DateTime())->d;
    if($dias < 2)
      echo "Produto e novo";
    else
      echo "produto velho"
  • Needs to be with date interval cannot be via PHP?

  • Or you just want a Countdown?

  • It is not simple to calculate the difference of the date of creation and today’s date?

  • can also be via php and I don’t know which one would be the easiest to do. and type and only one Countdown also because I want it to take today’s date and count 15 days

  • how can I calculate the difference of dates I’m seeing tutorials on the internet and type only think of this model: $data_initial = '2013-08-01'; $data_final = '2013-08-16'; // Calculates the difference in seconds between dates $difca = strtotime($data_final) - strtotime($start_date); //Calculates difference in days $days = floor($difference / (60 * 60 * 24)); echo "The difference is $days between";

3 answers

1


You can take the product creation date and calculate the difference(compared to today) with diff()

<?php

$produto = new stdClass();
$produto->dataCriacao = new DateTime('2015-10-01');

$dias = $produto->dataCriacao->diff(new DateTime())->d;
if($dias >= 15){
    echo "produto velho, adiciado há $dias dias";
}else{
    echo "produto adicionado há $dias dias";
}

Or in a simplified way:

$criacao_produto = new DateTime('2015-10-01');
$dias = $criacao_produto->diff(new DateTime())->d;
echo "criado há $dias dias";
  • Let me try to help you there. within that $product->dateCreation = new datetime(' '); should I pass my variable that I’m recovering from the database or is it the variable that is saving the date? Why $product = new stdClass(); this should not be used as a class instance?

  • @Leonardocosta is right, I created a product object, da para fazer de forma procedural, I already change the question.

  • apparently everything was fine now and wait the days to see if it won’t go wrong

  • Wait like that? Will it run in a scheduled routine by cron job or something? @Leonardocosta

  • like run the days I put a product today so I’ll wait until Sunday for it to run 2 days then it has to leave the new and enter the old I’ll show you how it turned my code vo edit the question

  • ready I edited the question from a look

Show 1 more comment

1

If you want MYSQL you have DATEDIFF. Use function.:

DATEDIFF(data1,data2)

will round off the days of difference.

Example of use.:

SELECT DATEDIFF('2016-10-09','2016-10-01') AS QuantidadeDias

Result = 8

To the example you gave:

Select CASE WHEN  DATEDIFF(CURDATE(),DataProduto)<15 THEN 'Sim' ELSE 'Não' END AS Novo

0

Browser other questions tagged

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