I heard the Messenger of Allah ﷺ say, "Whoever is in the mosque waiting for the prayer is in prayer."
أخبرنا قتيبة قال حدثنا بكر بن مضر عن عياش بن عقبة أن يحيى بن ميمون حدثه قال سمعت
سمعت رسول الله ﷺ يقول من كان في المسجد ينتظر الصلاة فهو في الصلاة
[Its chain is hasan] Ahmad narrated it: 5/331, from the hadith of Ayyash, through him, and it is in Kubra, no. 813, and Ibn Hibban graded it sahih, nos. 423, 424.