In Genesis 37, it says that Joseph was 17 when he had a great dream and his brothers hated him and then he was sold and brought to Egypt:
Genesis 37:2 these are the generations of Jacob. Joseph, being seventeen years old, was feeding the flock with his brethren; and he was a lad with the sons of Bilhah, and with the sons of Zilpah, his father’s wives: and Joseph brought the evil report of them unto their father….5 Now Joseph had a dream, and when he told it to his brothers they hated him even more…..28 Then Midianite traders passed by. And they drew Joseph up and lifted him out of the pit, and sold him to the Ishmaelites for twenty shekels of silver. They took Joseph to Egypt.
Then it says that when Joseph stood before the Pharaoh he was 30:
Genesis 41:46 And Joseph was thirty years old when he stood before Pharaoh king of Egypt.
Then the 7 years of plenty ended:
Genesis 41:53 And the seven years of plenty, that was in the land of Egypt, came to an end.
Then 2 years from the famine passed when he invited his brothers to Egypt:
Genesis 45:6 For these two years hath the famine been in the land: and there are yet five years, in which there shall be neither plowing nor harvest…9 Hurry and go up to my father and say to him, ‘Thus says your son Joseph, God has made me lord of all Egypt. Come down to me; do not tarry. 10 You shall dwell in the land of Goshen, and you shall be near me, you and your children and your children’s children, and your flocks, your herds, and all that you have.
From the above, we can conclude that the time between when Joseph’s was sold and brought to Egypt, and when he invited his family to come to Egypt will be= 2+7+30-17=22 years
Let’s see what happened in Jacob’s family within that time:
Judah married and begat Er and Onan (Genesis 38:3-4), this happens at least in 2 years for marrying and conceiving twice.
Then Er married Tamar, then he died, which nearly happens within 12 years till Er marries and let’s assume that he died immediately after he married, so we can have now about 14 years.
Afterwards, Onan married her and then died. Let’s assume that all this didn’t take one day. Then in verse 11 Tamar waited for Shelah to grow up, so that she marries him. But he grew up and didn’t marry her as in verse 14, then Judah sinned with her, and she begat Perez, this would take about 2 years. So we now have 16 years since Joseph was brought to Egypt,
So we can now conclude that Perez was about 6 years old when he went with the family of Jacob in Egypt. However, the Bible tells that when Perez went to Joseph, he had 2 sons:
Genesis 46:12 And the sons of Judah: Er, and Onan, and Shelah, and Perez, and Zerah; but Er and Onan died in the land of Canaan. And the sons of Perez were Hezron and Hamul.
How come that a boy at 6 years old not only that he marries, but he also has 2 sons? Only the Bible can answer!