I read somewhere that gravity affects time.
Please let me know the science behind it.
The first thing that you need to understand is that gravity is an effect of space-time warp which can act like very much like an elastic sheet which can even create waves (ref: Gravitational Waves).
The effect that creates these time dilations is called gravitational time dilation. This effect measures the amount of time that has elapsed between two events by observers at different distances from a gravitational mass. In other words, time runs slower wherever gravity is strongest, and this is because of gravity curves space-time. The curved space has more length than a straight line and hence light takes more time to go through that whole curvature between two points in space as compared to the straight line (ref: Gravitational time dilation.)