عَنْ أَنَسِ (بْنِ مَالِكٍ) رَضِيَ اللَّهُ عَنْهُ قَالَ: كَانَ النَّبِيُّ صَلَّى اللَّهُ عَلَيْهِ وَآلِهِ وَسَلَّمَ إِذَا كَانَ مُقِيمًا اعْتَكَفَ الْعَشْرَ الْأَوَاخِرَ مِنْ رَمَضَانَ وَإِذَا سَافَرَ اعْتَكَفَ مِنَ الْعَامِ الْمُقْبِلِ عِشْرَيْنَ
Sayyiduna Anas bin Malik (may Allah be pleased with him) narrates that when the Noble Prophet (peace and blessings be upon him) was residing, he would observe i'tikaf during the last ten days of Ramadan, but if he traveled during this period, then the following year he would observe i'tikaf for twenty days.