The exact right time to book a rental car is an ever-changing thing — there is no formula to use to guarantee the lowest price, unfortunately. The general consensus is that unless you have lots of flexibility, it’s probably best to book as soon as you can.
However, if you can afford to wait (and have no preference for car type or rental agency) you may be able to get a lower price closer in. Waiting till the last minute to book does have some risk — cars can sell out and agencies can raise prices anytime. Even if you wait till the last minute, you should book online! Booking at the counter will almost always be more expensive.