The temperature profile of a planetary atmosphere is a key diagnostic of radiative and dynamical processes governing the absorption, redistribution and emission of energy. Observations have revealed dayside stratospheres that either cool1,2 or warm3,4 with altitude for a small number of gas giant exoplanets, whereas other dayside stratospheres are consistent with constant temperatures5-7. Here we report spectroscopic phase curve measurements for the gas giant WASP-121b (ref. 8) that constrain stratospheric temperatures throughout the diurnal cycle. Variations measured for a water vapour spectral feature reveal a temperature profile that transitions from warming with altitude on the dayside hemisphere to cooling with altitude on the nightside hemisphere. The data are well explained by models assuming chemical equilibrium, with water molecules thermally dissociating at low pressures on the dayside and recombining on the nightside9,10. Nightside temperatures are low enough for perovskite (CaTiO3) to condense, which could deplete titanium from the gas phase11,12 and explain recent non-detections at the day-night terminator13-16. Nightside temperatures are also consistent with the condensation of refractory species such as magnesium, iron and vanadium. Detections15-18 of these metals at the day-night terminator suggest, however, that if they do form nightside clouds, cold trapping does not efficiently remove them from the upper atmosphere. Horizontal winds and vertical mixing could keep these refractory condensates aloft in the upper atmosphere of the nightside hemisphere until they are recirculated to the hotter dayside hemisphere and vaporized.