如何计算每月的逾期费用

时间:2018-06-09 06:38:22

标签: php

我遇到了逾期计算的问题,因为我不知道如何获得我设置的日期的月间隔。

我想在今天的日期到达逾期日期时,将余额增加3%。

这是我的代码:

<?php 
	$balance = $fetch['total'] - $total;
	$loan_date = strtotime($fetch['loan_end']);
	$TodayDate = strtotime('today');
  
  <!--plus 3% to the remaining for every month exceeds to loan_date then print the balance below-->
	
	print number_format($balance);
  
  
?>

以下是我每30天的示例时间间隔超过loan_end

       $invoicedate = strtotime("2013/07/01");
       $TodayDate = strtotime('today');

       $timeDiff = abs($TodayDate - $invoicedate);

       $numberDays = $timeDiff/86400;  // 86400 seconds in one day

       $numberDays = intval($numberDays);

       $noOfdaysToCheck ="30";

       $Fees ="600";

       if ($numberDays >= $noOfdaysToCheck){

              $Interval = $numberDays%$noOfdaysToCheck;

              for($i=1;$i<=$Interval;$i++){
                     $late = (1.5 / 100) * $Fees;
                     $Fees =  FeesCalc($Fees);

               }
        }

       $Fees = number_format($Fees, 2, '.', '');
       echo $Fees;

       function FeesCalc($Fees){
            $late = (1.5 / 100) * $Fees;
            return $TotalFees = $late+$Fees;

       }

计算超过30天的费用。现在在我的代码中我想计算(每天加3+余额)超过贷款结束。

2 个答案:

答案 0 :(得分:2)

而不是使用strtotime(),而是使用PHP的DateTime对象。他们有一个内置的方法来计算两个对象之间的月数:

<?php

$balance = $fetch['total'] - $total;
$endDate = new DateTime( $fetch['loan_end'] );
$diff    =  $endDate->diff( new DateTime() );
$months  = round( abs($diff->y * 12 + $diff->m + $diff->d / 30) );

$base    = $balance * 0.03;

$interest = $base * $months;

echo number_format( $interest + $balance );

请注意,此函数假定$fetch['loan_end']是MySQL DATE字段(例如2014-07-01)。

答案 1 :(得分:1)

请尝试以下代码:

&#13;
&#13;
<?php
	$balance = $fetch['total'] - $total;
	$endDate = new DateTime($fetch['loan_end']);
	$today = new DateTime();

	if($today >=$endDate)
		{
			$interval = $endDate->diff(new DateTime());
                        $months = round(abs($interval->y * 12 + $interval->m + $interval->d / 30));
			for($i=1;$i<=$months;$i++){
			$late = (3 / 100) * $balance;
			$balance = FeesCalc($balance);
			}
		}

	print number_format($balance);
  
  function FeesCalc($balance){
     $late = (3 / 100) * $balance;
     return $TotalFees = $late+$balance;
   }

?>
&#13;
&#13;
&#13;