@Ronald ********
most offices allow you to apply when you have 30 days left on your stamp. Some bigger offices like Bangkok, Chiang Mai and Phuket allow you to apply when you have 45 days left, so if there you can go right away.
A couple of offices (Koh Samui, Jomtien) want you to come in when you're on the last week of your stay.