Home > OS >  Daily increase on amount updates
Daily increase on amount updates

Time:06-23

I want to be able to increment a fee for each day after the first day for 100 days, which increases by .25.

This is my attempt, but I know I'm not doing it correctly:

$startday = new DateTime('2022-06-22');
$today = new DateTime();
$days  = $today->diff($startday)->format('%a');

echo $value = 100   $days * 025;

CodePudding user response:

So the base late fee is simply $1/day, and the $0.25/day increasing penalty.

The simple version is:

function lateFee($days) {
    $base = $days;
    $penalty = 0;
    for( $i=1; $i<$days;   $i ) {
        $penalty  = 0.25 * $i;
    }
    
    return $base   $penalty;
}

But it can also be condensed down to:

function lateFee($days) {
    return $days   array_sum(range(1, $days-1)) * 0.25;
}

CodePudding user response:

The easiest way to go about this would to be to create a loop, since you're building on a previous day's fee.

$startday = new DateTime('2022-06-01');
$today = new DateTime();
$days = $today->diff($startday)->format('%a');

$fee = 0;
if($days) {
    $i = 0;
    do {
        $fee  = 1.00   (.25 * $i);
        $i  ;
    } while($i < $days && $i <= 100);
}
echo "Fee for $days days is $fee";
  • Related