Count how many days you have today for the bank date

3

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

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

    
asked by anonymous 28.09.2017 / 15:45

3 answers

2

If you want to use MySQL itself, sometimes the query is faster because it does not need manipulation by PHP:

SELECT DATEDIFF(NOW(), DATA_TABELA) AS DIFERENCA_DIAS FROM TABELA_DATAS

Reference: link

    
28.09.2017 / 15:50
1

One of the ways to do this is by using the DateTime class, it has the diff method that returns an object DateInterval , which represents the interval between two distinct dates:

Following the example 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";

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

    
28.09.2017 / 15:49
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);
    
28.09.2017 / 20:27