How long will the sun last?

Our Sun is about 4.57 billion years old. About half way through it's life. The Sun will slowly turn into a red giant in about 4.5 billion year and about two billion years later, it will shed it's outer layers and become a white dwarf.

For the Earth, this means that when the Sun turns into a red giant, the Suns envelope will expand until it nearly touches the Earth.

For life on Earth, it's not as simple, in about a billion years time, the Sun will have increased it's temperature to such a degree that it will be impossible for life to exist on the surface as it would have become so hot that all water would have boiled away.

See related link for more information

The information I understand is the Earth is 4.5 billons years of age. From what I understand the formation of a galaxy can take upwards to .7 billion years. If so the completion of the Earth plus the age of the Earth would be 5.2 billion years. A yellow dwarf star should expand into a red dwarf at 6 billion years. As the sun expands into a red dwarf it will easily encompass the Earth along with planets as far away as Mars. This phenomenon could occur as early as 800 million years from now. Although some theories state as the red dwarf expands the Earth may be knocked backward possibly to a safe distance from the sun. The likelihood is if even this should occur, the distance of the Earth from the red dwarf would not be conducive to life on the planet. This is of course if this does even happen as the expectancy is the Earth when encompassed by the sun will melt into nothingness except for perhaps some vapors which seep into the solar system.