Mishkat al-Masabih 776
وَعَن أبي جهيم قَالَ: قَالَ رَسُولُ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسلم: «لَوْ يَعْلَمُ الْمَارُّ بَيْنَ يَدَيِ الْمُصَلِّي مَاذَا عَلَيْهِ لَكَانَ أَنْ يَقِفَ أَرْبَعِينَ خَيْرًا لَهُ مِنْ أَنْ يَمُرَّ بَيْنَ يَدَيْهِ» . قَالَ أَبُو النَّضر: لَا أَدْرِي قَالَ: «أَرْبَعِينَ يَوْمًا أَوْ شَهْرًا أَو سنة»
And from Abu Juhaym, who said: The Messenger of Allah ﷺ said: "If the one who passes in front of someone praying knew what [sin] is upon him, it would be better for him to stand for forty than to pass in front of him." Abu an-Nadr said: I do not know whether he said forty days, or months, or years.