I had this happen on the way out of Thailand and the immigration officer told me I would be best getting a visa before my next visit. I did that and next time I came to Thailand, the immigration officer asked why I got a visa when I was only staying a week.