Sunan Ibn Majah, Volume 1, Book 4, Hadith 753 (DarusSalam)

It was narrated that Abu Dharr Al-Ghifari said: "I said: 'O Messenger of Allah! Which mosque was built first?' He said: 'Al-Masjid Al-Haram (in Makkah).' I said: 'Then which?' He said: 'then Al-Masjid Al-Aqsa (in Jerusalem).' I said: 'How many years between them?' He said: 'Forty years, but the whole earth is a mosque for you, so pray wherever you are when the time for prayer comes.'"

حَدَّثَنَا عَلِيُّ بْنُ مَيْمُونٍ الرَّقِّيُّ، حَدَّثَنَا مُحَمَّدُ بْنُ عُبَيْدٍ، ح وَحَدَّثَنَا عَلِيُّ بْنُ مُحَمَّدٍ، حَدَّثَنَا أَبُو مُعَاوِيَةَ، عَنِ الأَعْمَشِ، عَنْ إِبْرَاهِيمَ التَّيْمِيِّ، عَنْ أَبِيهِ، عَنْ أَبِي ذَرٍّ الْغِفَارِيِّ، قَالَ قُلْتُ يَا رَسُولَ اللَّهِ أَىُّ مَسْجِدٍ وُضِعَ أَوَّلُ قَالَ ‏"‏ الْمَسْجِدُ الْحَرَامُ ‏"‏ ‏.‏ قَالَ قُلْتُ ثُمَّ أَىٌّ قَالَ ‏"‏ ثُمَّ الْمَسْجِدُ الأَقْصَى ‏"‏ ‏.‏ قُلْتُ كَمْ بَيْنَهُمَا قَالَ ‏"‏ أَرْبَعُونَ عَامًا ثُمَّ الأَرْضُ لَكَ مُصَلًّى فَصَلِّ حَيْثُ مَا أَدْرَكَتْكَ الصَّلاَةُ ‏"‏ ‏.‏

sunnah.com