Count how many days from today to the bank date

Asked

Viewed 154 times

2

I need to create a PHP application that reads the date of a column inside the MYSQL database and compare with today’s date, after comparing it should print the difference of days.

Example: In the bank contains the following date:2017-08-31, in the column 'dates'. As today is day 2017-09-28, he would have to return me 29 days.

3 answers

3


  • This AS DIFERENCA_DIAS, what would it be? I don’t understand.

  • It is an ALIAS for the name of the field that will contain the number 29, according to your example. Ai you call in PHP the result value as $difca = $S['DIFERENCA_DIAS']; https://www.w3schools.com/sql/sql_alias.asp

  • I did it your way and it worked perfectly, thank you very much!

1

One of the ways to do this is by using the class Datetime, it has the diff method that returns an object DateInterval, representing the interval between two separate dates:

Following the example of dates:

$data1 = new DateTime( '2013-12-11' );
$data2 = new DateTime( '1994-04-17' );

$intervalo = $data1->diff( $data2 );

echo "Intervalo é de {$intervalo->y} anos, {$intervalo->m} meses e {$intervalo->d} dias";

Response from: How to calculate the difference between two dates?

1

$dt_hoje   = date('Y-m-d');
$dt_banco  = '2017-08-22';
// Usa a função strtotime() e pega o timestamp das duas datas:
$dt_hoje   = strtotime($dt_hoje);
$dt_banco  = strtotime($dt_banco);
// Calcula a diferença de segundos entre as duas datas:
$diferenca = $dt_banco - $dt_hoje;
// Calcula a diferença de dias
$dias      = (int)floor( $diferenca / (60 * 60 * 24));
print_r($dias);

Browser other questions tagged

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