There is no direct train from Hat Yai to Kanchanaburi, but you can easily make this rail journey by taking the train to Nakhon Pathom and changing there.
The first step of the journey is to choose one of five overnight sleeper trains from Hat Yai Junction to Nakhon Pathom.
|42*||--:--||--:--||Sp. Ex. DRC||Info >|
|32||17:45||09:29||Sp. Ex.||Info >|
|38 / 46||18:15||10:48||Sp. Ex.||Info >|
* Train 42 is currently not in service.
Rap = Rapid
Sp. Ex. = Special Express
Sp. Ex. DRC = Special Express Diesel Railcar - (Sprinter Train)
Ticket prices from Hat Yai Junction to Nakhon Pathom vary considerably.
Tickets from Nakhon Pathom to Kanchanaburi cost a fixed tourist fee of 100 THB.
You will have to take into account Thai trains are not known for their punctuality, so the best choices are the DRC Special Express or Rapid trains from Hat Yai to Nakhon Pathom (that should hopefully give you enough of a gap to make the morning train to Kanchanaburi).
Another option is to plan to spend some time in Nakhon Pathom.
Nakhon Pathom claims to be one of the oldest towns in Thailand and is quite a pleasant place to visit.
It is famous for having the worlds largest Chedi (Phra Pathom Chedi is an easy 400 - 500 metres walk straight down the road from Nakhon Pathom Railway Station).
You could then take one of the later trains from Hat Yai, spend the morning seeing the sights of the old city, have lunch, then take the afternoon train to Kanchanburi.
Booking advance tickets is highly advisable for the sector from Hat Yai to Nakhon Pathom, especially if you want to get a good sleeping berth.
You can book tickets from any SRT railway station in Thailand or go to the advance booking office at Hat Yai Junction Station as soon as you know the date you want to travel.
For the sector between Nakhon Pathom and Kanchanburi, you can only buy tickets for these Ordinary trains on the day of departure from Nakhon Pathom Train Station.