First assume annual payments. If each payment is $P$ then initial loan balance $C=\frac{P}{r}\left(1-\frac{1}{(1+i)^D}\right).$ If amount $r$ is reimbursed after $t$ years, then remaining loan balance $C-r=\frac{P}{r}\left(1-\frac{1}{(1+i)^{D-t}}\right)$. Taking ratio of the two equations, $1-\frac{r}{C}=\frac{1-\frac{1}{(1+i)^{D-t}}}{1-\frac{1}{(1+i)^{D}}}$ or $1+\frac{r}{C}\left((1+i)^{D}-1 \right)=(1+i)^{t}$ or $t=\frac{\ln\left(1+\frac{r}{C}\left((1+i)^{D}-1 \right) \right)}{\ln(1+i)}$.
If instalments are monthly, the number of payments and the interest rate are adjusted so the new formula for the number of months in which amount $r$ is reimbursed is
$t=\frac{\ln\left(1+\frac{r}{C}\left((1+\frac{i}{12})^{12\times D}-1 \right) \right)}{\ln(1+\frac{i}{12})}$