user3436588
user3436588

Reputation: 15

php date difference & integer calculate

i do have a lot of problem in follow code. first, I try to get number of days between two date and try to multiply with some integer. However, it doesn't multiplied. How can I get correct answer?

Help me guys.

function GetTotalDay($D1, $D2)
{
    //$str = "19-04-2016";
    $str = strtotime($D2) - (strtotime($D1));
    echo (int)floor($str/3600/24);
}

$C1=GetTotalDay("01/01-2016", "03-01-2016");
$C2=12;
echo $C1 * $C2;

Upvotes: 0

Views: 63

Answers (2)

Ramalingam Perumal
Ramalingam Perumal

Reputation: 1427

You have 2 mistakes.

1) You use incorrect formats of date

2) You have no return in function

<?php
function GetTotalDay($D1, $D2)
{
    //$str = "19-04-2016";
    $str = strtotime($D2) - (strtotime($D1));
    return (int)floor($str/3600/24);
}

$C1=GetTotalDay("01-01-2016", "03-01-2016");
$C2=12;
echo $C1 * $C2;
?>

Result :

24

Upvotes: 0

Gokul Shinde
Gokul Shinde

Reputation: 965

Check this corrected code

function GetTotalDay($D1, $D2) {
  $str = strtotime($D2) - (strtotime($D1));
  return (int)floor($str/3600/24);
}

$C1 = GetTotalDay("01-01-2016", "03-01-2016");
$C2 = 12;
echo $C1 * $C2;

Upvotes: 1

Related Questions