Apparently the mosques in Mecca and Jerusalem were built 40 years apart:
Abu Dhar narrated: I said, "O Allah's Apostle! Which mosque was first built on the surface of the earth?" He said, "Al-Masjid-ul-Haram (in Mecca)." I said, "Which was built next?" He replied "The mosque of Al-Aqsa (in Jerusalem)." I said, "What was the period of construction between the two?" He said, "Forty years." He added, "Wherever (you may be, and) the prayer time becomes due, perform the prayer there, for the best thing is to do so (i.e. to offer the prayers in time)."
Sahih al-Bukhari: Volume 4, Book 55, Hadith Number 585
How can this be? Abraham (who built the Kaabah) and Solomon (who built the temple in Jerusalem) lived hundreds of years apart. What actual events is the Prophet referring to?