Accurate analytical solutions to nonlinear oscillators by means of the Hamiltonian approach