Violations of CPT symmetry and Lorentz invariance are searched for by studying interference effects in B0 mixing and in Bs0 mixing. Samples of B0→J/ψKS0 and Bs0→J/ψK+K− decays are recorded by the LHCb detector in proton--proton collisions at centre-of-mass energies of 7 and 8 TeV, corresponding to an integrated luminosity of 3 fb−1. No periodic variations of the particle-antiparticle mass differences are found, consistent with Lorentz invariance and CPT symmetry. Results are expressed in terms of the Standard Model Extension parameter Δaμ with precisions of O(10−15) and O(10−14) GeV for the B0 and Bs0 systems, respectively. With no assumption on Lorentz (non-)invariance, the CPT-violating parameter z in the Bs0 system is measured for the first time and found to be Re(z)=−0.022±0.033±0.005 and Im(z)=0.004±0.011±0.002, where the first uncertainties are statistical and the second systematic.