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 the whole earth is a place of prayer for you, so pray wherever the time of prayer overtakes you."
حدثنا علي بن ميمون الرقي حدثنا محمد بن عبيد ح و حدثنا علي بن محمد حدثنا أبو معاوية عن الأعمش عن إبراهيم التيمي عن أبيه
قلت يا رسول الله أي مسجد وضع أول قال المسجد الحرام قال قلت ثم أي قال ثم المسجد الأقصى قلت كم بينهما قال أربعون عاما ثم الأرض لك مصلى فصل حيث ما أدركتك الصلاة
Bukhari narrated it, Hadiths of the Prophets, chapter 10, no. 3366 and no. 3425, and Muslim, Mosques, chapter on Mosques and Places of Prayer, no. 520, from the hadith of Amash through the same chain.