calculation of days between dates php [duplicate]

1

I'm trying to figure out what the difference is in days, based on 2 dates.

Well my code looks like this:

$data_inicio = new DateTime("2016-07-10");
$data_fim = new DateTime("2016-07-13");

// Resgata diferença entre as datas
$dateInterval = $data_inicio->diff($data_fim);
$dias = $dateInterval->d + ($dateInterval->y * 12);

echo $dias;

Good if I inform the following values:

$data_inicio = new DateTime("2016-07-10");
$data_fim = new DateTime("2016-07-13");

My return and 3, until then everything is ok. But when I post:

$data_inicio = new DateTime("2016-07-10");
$data_fim = new DateTime("2017-08-13");

My return is still 3, meaning the system ignored the months and years. Anyone know how to solve this?

    
asked by anonymous 13.07.2016 / 20:20

2 answers

7

You can do this:

<?php 
    $data_inicio = new DateTime("2016-07-10");
    $data_fim = new DateTime("2017-07-10");

    // Resgata diferença entre as datas
    $dateInterval = $data_inicio->diff($data_fim);
    echo $dateInterval->days;

    //365
 ?>

It has a difference between d and days you can see the here

    
13.07.2016 / 20:41
2

You can use this function:

<?php
$date1=date_create("2016-07-10");
$date2=date_create("2017-08-13");
$diff=date_diff($date1,$date2);
echo $diff->format("%a");
?>

//Saída: 399 
    
13.07.2016 / 20:37