If you're planning a trip and looking for the best deal on airfare, you don't want to book too far in advance.

Stephen Brashear, Getty Images

That's the message from CheapAir.com, which studied 560 million 2012 fare records. In fact, the best time to buy a ticket for domestic travel is 49 days before departure and 81 days before international flights.

"There were a lot of discrepancies between different cities and dates, so it's not a hard and fast rule that you can always buy your ticket 49 days in advance," said Jeff Klee, CEO of CheapAir.com. "But what is consistent is that buying tickets too late will cost you. The day before a flight is the worst day to buy a ticket, two days before a flight is the second worst, etc. up to about ten days out."

Buying tickets too early will also cost you.

"We started looking at fares booking 210 days in advance and that was the worse behind booking ten days in advance. So really, there's a sweet spot in the middle and that's where you want to be," said Klee. "Airlines generally keep fares high far in advance. They don't normally run sales too far in advance. They generally let flights go for a while and see how advance bookings go. As it gets closer, they start to consider how aggressively they need to discount fares on a particular flight to sell whatever seats are left."

If you're traveling during a holiday or during any other peak time, the airlines are under much less pressure to aggressively discount flights. In that case, it's best to book more than 49 days in advance.

"On the other hand, if you're traveling on a Tuesday or a Wednesday during a slow time of year to a destination that's not that popular, you can probably go inside the 49 days," said Klee.

As for the best days to fly, Tuesday and Wednesday are the least popular travel days and you can often find the best deals on those days.

"On average, if you're taking a seven day trip, a Tuesday to Tuesday trip would be about $56 dollars less than a Sunday to Sunday trip. Sunday is the most expensive day to fly. Tuesday is the least expensive. You can also get good deals on Wednesday. Friday is pretty bad, while Saturday, Monday and Thursday are in the middle," said Klee.