Is it true that if you want a boy you would have to have sex closer to ovulation and for a girl you need to have sex few days before?

Myth. This has to do with the idea that male and female sperm swin at different speeds, so to speak. But it is virtually impossible to determine exactly when an egg is released and when conception occurs. It is a 50-50 proposition and you "get what you get".
Sort of. It is generally true that sperm carrying the y chromosome swim faster but also don't live as long. "girl sperm" seem to travel slower, but live longer. Once ovulation occurs, the egg doesn't live long. So whichever sperm are there when the time comes are the winners. Unfortunately there is a lot of overlap, so this method is by no means perfect. Good luck!