calculation between two defined dates

Asked

Viewed 70 times

1

I have the following code on 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;

He returns to me what was the cost of the day 01/06 until 15/06, on a basis of 120.00 per 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

Someone knows how to do it?

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));

?>

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

  • very good thank you

Browser other questions tagged

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