Calculate Your Time to Retirement
The most accurate method is to generate a spreadsheet that shows your present and future cash flows on a year-by-year or even month-by-month basis. This way it becomes possible to account for special cases such as the mix between taxable and tax-deferred accounts, rule 72(t) and other regulations, increases in health care costs, etc.
It is possible to derive equations that capture this in a very simplified manner. Such equations give the impression of precision but they are not very accurate.
The simplest one assumes a fixed safe withdrawal rate and a fixed savings rate. If the SWR is 4%/year, the number of years of savings needed is 1/0.04 = 25 years. If the savings rate is r, the spending rate is necessarily 1-r. Each year of working thus allows one to save r/(1-r) years of spending. For example, if the savings rate is 20%, it takes 4 years of saving that sum to have enough money to afford one year of spending.
Combining these two ideas give the equation working years=(1/SWR)/(r/(1-r)).
For example, if SWR=4% and r=80%, then working years = 25*0.2/0.8 = 6.25 years.
This equation ignored compound interest during the accumulation phase and thus it does not work for longer time spans where compounding makes a difference. It does account for inflation as this effect is included in the SWR.
A more complicated equation that considers the effect of compound interest is given in the Early Retirement Extreme Book