calculation between two defined dates

1

I have the following code in PHP :

$valor = 120;
$data_inicio = '2016-06-15';

$total_dias = date('t', strtotime($data_inicio));
$used = date('d', strtotime($data_inicio));

$result = ($valor/$total_dias)*$used;

echo $result;

It returns me what was the amount spent for the day 01/06 by 15/06 , with a basis of 120,00 by month.

I wanted to know how to do this with 2 different dates.

Example:

$data1 = '2016-06-16';
$data2 = '2016-07-01';

The result has to be 56.00

Does anyone know how to do this?

    
asked by anonymous 09.06.2016 / 18:54

1 answer

6
<?php

    function dateDiff($firstDate, $lastDate) {
        $firstDate = new DateTime($firstDate);
        $lastDate = new DateTime($lastDate);

        $intervalo = $firstDate->diff($lastDate);
        print "{$intervalo->y} anos, {$intervalo->m} meses e {$intervalo->d} dias"; 
    }
    $data1 = '2016-06-16';
    $data2 = '2016-08-01';

    print_r(dateDiff($data1, $data2));

?>

Certainly this code will help you work with the differences between dates since you use the total days and days of difference in the variable called: $ used .

    
09.06.2016 / 20:54