I asked, "Messenger of Allah, which mosque was built first?"He replied, "The Sacred Mosque."I asked, "Then which?"He said, "Then the Aqsa Mosque."I asked, "How long was there between them?"He answered, "Forty years. And wherever the time of prayer overtakes you, pray, for it is a mosque."
حدثنا وكيع عن سفيان عن الأعمش عن إبراهيم التيمي عن أبيه
قلت يا رسول الله أي مسجد وضع أول قال المسجد الحرام قال قلت ثم أي قال ثم المسجد الأقصى قال قلت كم بينهما قال أربعون سنة ثم أينما أدركتك الصلاة فصل فهو مسجد
Its isnad is sahih, it has already appeared at 21279.21286m - Ubayda narrated to us... and he related it, except that he said, "Which masjid was placed on the earth first?" Sahih (21286m). Its isnad is sahih. Ubayda is Ibn Humayd al-Dabbi al-Haddha an-Nahwi, the well-known trustworthy narrator, whose hadith appears in Bukhari. This hadith is a continuation of the one before it.