Formation of planets during the evolution of single and binary stars