What is the function/formula for calculating the term of a loan based on payment?

Knowing the Loan amount, APR and Payment, I need to create a calculator that allows me to adjust the term based on various payment amounts.

Welcome!
May be so?
image

That’s what I thought but the result seemed wrong so I thought I might have been using the wrong one. Thank you for confirming.