Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading