We model the optimal copulation duration in the yellow dungfly Scathophaga stercoraria, assuming that males optimize their reproductive success per day. The independent state-variables of a male are the actual sperm reserves, the female encounter rate and the time of the day. We used stochastic dynamic programming to predict the optimal copulation duration. The model predicts that copulation duration should increase (i) for larger males, (ii) for males with a better previous diet, (iii) for males accepting more females, (iv) for males staying away from the dung during a whole day. It also predicts that (v) males on a restricted diet should accept fewer matings, (vi) testes size should shrivel during the day, (vii) the correlation coefficient between copulation duration and sequence number should decrease with increasing initial sperm reserve and with increasing number of copulations performed on a given day. There are two essential differences between our model and a model of Parker. (i) Our model assumes that males optimize per day. Parker's model assumes that males optimize per mating bout, one bout comprising many days. (ii) Since all the important state variables depend only on the moment of the copulation, our model does not assume a long-time memory in dungflies, and it is more flexible than Parker's model. Overall, both models explain the data available equally well.