src/Finance.php
public static function payback(array $values, float $rate = 0.0): float
    {
        $last_outflow = -1;
        for ($i = 0; $i < sizeof($values); $i++) {
            if ($values[$i] < 0) {
                $last_outflow = $i;
            }
        }

        if ($last_outflow < 0) {
            return 0.0;
        }

        $sum = $values[0];
        $payback_period = -1;
        for ($i = 1; $i < sizeof($values); $i++) {
            $prevsum = $sum;
            $discounted_flow = $values[$i] / (1 + $rate)**$i;
            $sum += $discounted_flow;
            if ($sum >= 0) {
                if ($i > $last_outflow) {
                    return ($i - 1) + (-$prevsum / $discounted_flow);
                }
                if ($payback_period == -1) {
                    $payback_period = ($i - 1) + (-$prevsum / $discounted_flow);
                }
            } else {
                $payback_period = -1;
            }
        }
        if ($sum >= 0) {
            return $payback_period;
        }
        return \NAN;
    }
public static function payback(array $values, float $rate = 0.0): float
    {
        $last_outflow = -1;
        for ($i = 0; $i < sizeof($values); $i++) {
            if ($values[$i] < 0) {
                $last_outflow = $i;
            }
        }

        if ($last_outflow < 0) {
            return 0.0;
        }

        $sum = $values[0];
        $payback_period = -1;
        for ($i = 1; $i < sizeof($values); $i++) {
            $prevsum = $sum;
            $discounted_flow = $values[$i] / (1 + $rate)**$i;
            $sum += $discounted_flow;
            if ($sum >= 0) {
                if ($i > $last_outflow) {
                    return ($i - 1) + (-$prevsum / $discounted_flow);
                }
                if ($payback_period == -1) {
                    $payback_period = ($i - 1) + (-$prevsum / $discounted_flow);
                }
            } else {
                $payback_period = -1;
            }
        }
        if ($sum >= 0) {
            return $payback_period;
        }
        return \NAN;
    }
public static function payback ( array $ values , float $ rate = 0.0 ) : float { $ last_outflow = - 1 ; for ( $ i = 0 ; $ i < sizeof ( $ values ) ; $ i ++ ) { if ( $ values [ $ i ] < 0 ) { $ last_outflow = $ i ; } } if ( $ last_outflow < 0 ) { return 0.0 ; } $ sum = $ values [ 0 ] ; $ payback_period = - 1 ; for ( $ i = 1 ; $ i < sizeof ( $ values ) ; $ i ++ ) { $ prevsum = $ sum ; $ discounted_flow = $ values [ $ i ] / ( 1 + $ rate ) ** $ i ; $ sum += $ discounted_flow ; if ( $ sum >= 0 ) { if ( $ i > $ last_outflow ) { return ( $ i - 1 ) + ( - $ prevsum / $ discounted_flow ) ; } if ( $ payback_period == - 1 ) { $ payback_period = ( $ i - 1 ) + ( - $ prevsum / $ discounted_flow ) ; } } else { $ payback_period = - 1 ; } } if ( $ sum >= 0 ) { return $ payback_period ; } return \ NAN ; }
Discounted Payback of an investment.
The number of periods to recoup cash outlays of an investment.

This is commonly used with discount rate=0 as simple payback period,
but it is not a real financial measurement when it doesn't consider the
discount rate. Even with a discount rate, it doesn't consider the cost
of capital or re-investment of returns.

Avoid this when possible. Consider NPV, MIRR, IRR, and other financial
functions.

Reference:
https://en.wikipedia.org/wiki/Payback_period

The result is given assuming cash flows are continous throughout a period.
To compute payback in terms of whole periods, use ceil() on the result.

An investment could reach its payback period before future cash outlays occur.
The payback period returned is defined to be the final point at which the
sum of returns becomes positive.

Examples:
The payback period of an investment with a $1,000 investment and future returns
of $100, $200, $300, $400, $500:
payback([-1000, 100, 200, 300, 400, 500])

The discounted payback period of an investment with a $1,000 investment, future returns
of $100, $200, $300, $400, $500, and a discount rate of 0.10:
payback([-1000, 100, 200, 300, 400, 500], 0.1)

@param  array $values
@param  float $rate

@return float
Discounted Payback of an investment . The number of periods to recoup cash outlays of an investment .