Generally speaking, for regional and local trains in Italy you buy a ticket and validate it before boarding ("obliterare" in italian). If you can't find any validating machine, board the train and immediately go look for the conductor or ticket inspector to notify.
Tickets are usually one way, so you need one from A to B and one from B to A. Sometimes vending machines can sell the return fare and print two tickets.
Basic fare tickets have no boarding time restrictions, just use them before expiration (30 or 90 days after purchase).
How to get to "Ercolano scavi" then? Ercolano municipality official website has english instructions available too.
From Naples – Garibaldi Square, take the EAV – CIRCUMVESUVIANA company,
direction to Sorrento or Poggiomarino.There are two stops in Ercolano: the
ERCOLANO –SCAVI that is near the Archaeological ruins, and the ERCOLANO–
MIGLIO D’ORO that is near the ancient villas of Miglio d’oro, some of which are
visitable. The travel time is 20 minutes.
The Archaelogical site is 700 metres (about 0.5 miles) far from the ERCOLANO-SCAVI stop
You can look for timetables in their official website http://time1.eavsrl.it/ (yeah, no https)
You can arrive here also with TRENITALIA company in this case stop at Portici-Ercolano railway station. The archaeological ruins are 1.6 km far. (about 1 mile)
In this case you can buy tickets online on https://www.trenitalia.com/ from "Napoli Piazza Garibaldi" to "Portici-Ercolano". Fare is 1.6€ one way. If you buy online, your ticket will have date and time you indicated at time of purchase, no need to validate it when boarding.
This train stop is a little bit farther from the ruins, about double the distance.
- Plan extra time for delays and cancellations. Major italian economic newspaper "Il sole 24 ore" recently ranked EAV - Circumvesuviana 1st of the 10 worst railways of Italy.
- Make sure to bring enough water with you if travelling during spring/summer.
- Pay a lot of attention to street scammers, they are very prominent in all italian tourist destinations.
- If train is packed, remove your backpack when standing.
- Restrooms might not be available on train.