A theory of the time dependence of earthquake foreshocks and aftershocks is presented. The theory involves the response to sudden forcing of a dynamics of self-driven acceleration to failure. The empirically observed Omori law, which says that the rate of aftershocks as a function of time falls as a power law in time, is derived theoretically. The exponent of the falloff in time is shown to generically give a value close to one, for rapidly accelerating dynamics. To see if the theory is consistent with other features of real data, foreshocks and aftershocks of small magnitude mainshocks are analyzed in a catalogue of real earthquakes. Results show that the spatial and temporal distribution of aftershocks is separable into a dependence on space and a dependence on time, that the spatial distribution of aftershocks is consistent with the hypothesis that stress changes from the mainshock cause aftershocks, and that the number of foreshocks approaches the number of aftershocks as the magnitude of the mainshock becomes smaller.