We prove that a class of A-stable symplectic Runge–Kutta time semi-discretizations (including the Gauss–Legendre methods) applied to a class of semilinear Hamiltonian partial differential equations (PDEs) that are well posed on spaces of analytic functions with analytic initial data can be embedded into a modified Hamiltonian flow up to an exponentially small error. Consequently, such time semi-discretizations conserve the modified Hamiltonian up to an exponentially small error. The modified Hamiltonian is O(hp )-close to the original energy, where p is the order of the method and h is the time-step size. Examples of such systems are the semilinear wave equation, and the nonlinear Schrödinger equation with analytic nonlinearity and periodic boundary conditions. Standard Hamiltonian interpolation results do not apply here because of the occurrence of unbounded operators in the construction of the modified vector field. This loss of regularity in the construction can be taken care of by projecting the PDE to a subspace in which the operators occurring in the evolution equation are bounded, and by coupling the number of excited modes and the number of terms in the expansion of the modified vector field with the step size. This way we obtain exponential estimates of the form O(exp(–c/h 1/(1+q))) with c > 0 and q ⩾ 0; for the semilinear wave equation, q = 1, and for the nonlinear Schrödinger equation, q = 2. We give an example which shows that analyticity of the initial data is necessary to obtain exponential estimates.