The change comes after experts said 2040 would be too late if the UK wants to achieve its target of emitting virtually zero carbon by 2050.
Boris Johnson unveiled the policy as part of a launch event for a United Nations climate summit in November.
He said 2020 would be a "defining year of climate action" for the planet.
The summit, known as COP26, is being hosted in Glasgow. It is an annual UN-led gathering set up to assess progress on tackling climate change.
Sir David Attenborough said at the launch event at London's Science Museum that he was looking forward to COP26 and found it "encouraging" that the UK government was launching a "year of climate action".
"The longer we leave it... the worse it is going to get," he said.
"So now is the moment. It is up to us to organise the nations of the world to do something about it."
In a statement made ahead of the launch, Mr Johnson said the ban on selling new petrol and diesel cars would come even earlier than 2035, if possible.
Hybrid vehicles are also now being included in the proposals, which were originally announced in July 2017.
People will only be able to buy electric or hydrogen cars and vans, once the ban comes into effect.
The change in plans, which will be subject to a consultation, comes after experts warned the previous target date of 2040 would still leave old conventional cars on the roads following the clean-up date of 2050.
The Scottish government does not have the power to ban new petrol and diesel cars but has already pledged to "phase out the need" for them by 2032 with measures such as an expansion of the charging network for electric cars.
Mr Johnson said the 2050 pledge was necessary because the UK's "historic emissions" meant "we have a responsibility to our planet to lead in this way".
The announcement comes as COP26's former president Claire O’Neill, who was sacked on Friday, wrote a bitter letter accusing Mr Johnson of failing to support her work.
The prime minister's official spokesperson said Downing Street had "no comment" to make on the letter, but thanked Mrs O'Neill for her work towards the conference.
He said her replacement would be a "ministerial post" with details set out "in due course."
Mr Johnson did not answer the BBC's David Shukman's questions about the row.
Mr Johnson said: “Hosting COP26 is an important opportunity for the UK and nations across the globe to step up in the fight against climate change.
“As we set out our plans to hit our ambitious 2050 net zero target across this year, so we shall urge others to join us in pledging net zero emissions.
“There can be no greater responsibility than protecting our planet, and no mission that a global Britain is prouder to serve."
At the Science Museum the prime minister added that a "catastrophic period of global addiction" to hydrocarbons had led to the planet being "swaddled in a tea cosy" of carbon dioxide.
But Green Party MP Caroline Lucas said on Twitter: "Carbon emissions are not 'swaddling the planet like a tea cosy'. They are behind wildfires in Australia, soaring temperature records and the broken lives of those least responsible. The PM needs to understand that - and act."
Friends of the Earth's Mike Childs said the government was "right" to bring forward the ban, but that 2030 would be better than 2035.
“A new 2035 target will still leave the UK in the slow-lane of the electric car revolution and meantime allow more greenhouse gases to spew into the atmosphere," he said.
He said the government could show "real leadership" ahead of COP26 by reversing plans to develop "climate-wrecking roads and runways".
AA president Edmund King said: "Drivers support measures to clean up air quality and reduce CO2 emissions but these stretched targets are incredibly challenging."
The chief executive of the society of motor manufacturers and traders (SMMT) accused the government of "moving the goalposts".
"With current demand for this still expensive technology still just a fraction of sales, it's clear that accelerating an already very challenging ambition will take more than industry investment," Mike Hawes said.
He said the government's plans must safeguard industry and jobs, as well as ensuring current sales of low emission vehicles were not undermined.
Meanwhile Mrs O’Neill accused Mr Johnson of promising money and people to support her work, but failing to deliver either.
She also said he admitted to her that he "doesn't really get" climate change and claimed he had refused to give Scottish First Minister Nicola Sturgeon an official role at COP26.
Cabinet minister Michael Gove said Mrs O'Neill was a "close friend" but that he disagreed with her comments.
He told BBC Radio 5 Live Mr Johnson described his own political outlook as "that of a green Tory".
Mrs O'Neill said her "absolute desire for action has not been comfortable for some", adding that this was "not about me" or Mr Johnson - but about working towards "rapid decarbonisation".
She said at COP26 the UK must "absolutely double down on taking our great leadership and ambitions in this space, and really energising the world as to why this is a huge opportunity".
When life shuts a door ... open it again. It's a door. That's how they work.