Increment daily fee on number of days

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

So the first day will be $1.00 ($1.00)
The second day will be $2.25 ($1.00 + $1.25)
The third day will be $3.75 ($2.25 + $1.50)
The fourth day will be $5.50 ($3.75 + $1.75)

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;

>Solution :

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

Leave a Reply