Exploring the history of star formation in galaxies and its environmental dependence at high redshift