We present a study of the chemistry of a dense photon-dominated region (PDR) using a time-dependent chemical model. Our major interest is to study the spatial distribution of complex molecules such as hydrocarbons and cyanopolyynes in the cool dense material bordering regions where star formation has taken place. Our standard model uses a homogeneous cloud of density 2x10e4 cm-3 and temperature T=40 K, which is irradiated by a far-ultraviolet radiation field of intermediate intensity, given by X=100. We find that over a range of times unsaturated hydrocarbons (e.g., C2H, C4H, C3H2) have relatively high fractional abundances in the more external layers of the PDR, whereas their abundances in the innermost layers are several orders of magnitudes lower. On the other hand, molecules that are typical of late-time chemistry are usually more abundant in the inner parts of the PDR. We also present results for models with different density, temperature, intensity of the radiation field and initial fractional abundances. Our results are compared with both high- and moderate-angular resolution observations of the Horsehead nebula. Our standard model is partially successful in reproducing the observations. Additional models run with different physical parameters are able to reproduce the abundance of many of the observed molecules, but we do not find a single model that fits all the observations at the same time. We discuss the suitability of a time-dependent model of a dense PDR such as ours as an estimator of the age of a PDR, provided that enough observational data exist.