php日期差异&整数计算

时间:2016-04-20 09:04:04

标签: php

我在跟随代码中遇到了很多问题。首先,我尝试获取两个日期之间的天数,并尝试乘以一些整数。但是,它没有成倍增加。我怎样才能得到正确答案?

帮助我们。

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;

2 个答案:

答案 0 :(得分:1)

检查此更正后的代码

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;

答案 1 :(得分:0)

你有两个错误。

1)您使用的日期格式不正确

2)你没有功能上的回报

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

结果:

24
相关问题