WASHINGTON — President Donald Trump agreed to reopen all of the federal government Friday, yielding to Democratic demands by saying he would sign a bill to fund the shuttered agencies for three weeks and provide back pay to federal workers without receiving any money for his promised wall along the southern border.

The deal will reopen the government until Feb. 15, and includes a promise for House and Senate negotiations over border security. It will allow lawmakers to talk while the wheels of government begin turning again after a shutdown that lasted 35 days, the longest in U.S. history.

The Senate quickly approved the funding bill by a voice vote Friday afternoon and the House followed suit around 7 p.m. The president signed it shortly before 9:30.

The short-term agreement came hours after flight delays at LaGuardia and Newark Liberty Airports caused by a shortage of air-traffic controllers rippled throughout the Northeast, with delays of 16 to 30 minutes on departing flights at Philadelphia International Airport, where workers, union leaders, and lawmakers rallied for an end to the shutdown hours before the deal was announced.

“After 36 days of spirited debate and dialogue, I have seen and heard from enough Democrats and Republicans that they are willing to put partisanship aside, I think, and put the security of the American people first,” Trump said in a speech in the Rose Garden, where he was cheered by aides and said he was “proud” of an announcement that many on both the left and the right saw as a capitulation.

The deal did not move Trump closer to receiving funding for the wall, and Trump raised the possibility that the government might be shut down again in three weeks if he doesn’t get it. He also hinted at using executive powers to go around Congress to obtain money for the wall, after years of promising that Mexico would fund the project.

Democrats remained firm in their opposition to any money for what they see as an ineffective mechanism and a symbol of xenophobia. They have said they would support security measures centered on technology, like drones, and improved equipment to screen for drugs.

Directly and indirectly, they declared victory after the first test of the newly divided government.

“Hopefully it means a lesson learned,” said Senate Minority Leader Chuck Schumer (D., N.Y.), adding that shutting down the government “accomplishes nothing but pain and suffering for the country and incurs an enormous political cost to the party shutting it down.”

Referring to House Speaker Nancy Pelosi (D., Calif.), he later added, “No one should ever underestimate the speaker, as Donald Trump has learned.”

Pelosi said Democrats’ “unity is our power, and that is what maybe the president underestimated.”

It was not clear when Trump would deliver his State of the Union speech, originally scheduled for Tuesday. Pelosi canceled her invitation, citing the shutdown, and said Friday that they would work on finding a new date.

Although the government was set to reopen, uncertainty remained, with Trump and Democrats still far apart on border security and another deadline now looming.

“An endless shutdown would be a terrible thing, but so, too, would be an endless string of returning shutdowns,” said Katharine Young, an associate professor at Boston College Law School who has written about shutdowns.

The monthlong fight badly damaged Trump’s public standing without securing the money he wanted for a central campaign promise. He agreed to a temporary deal that matches what Democrats had been seeking for weeks, after the dispute shuttered roughly 25 percent of the federal government and inflicted economic and psychological strain on about 800,000 federal workers who missed two paychecks. Some had taken side jobs, spent savings, or utilized food banks to get by. Many feared damage to their credit from missed payments for car loans or mortgages.

The stare-down also hindered aid that helps people pay for food and housing; strained national parks; and burdened employees of the Coast Guard, the Transportation Security Administration, federal prisons, and other agencies where employees were forced to work without pay.

"It’s the most stupid shutdown I’ve ever seen in my life," Sen. Jon Tester (D., Mont.) told Politico. “He should go back to school and find out what civics is about and find out, if you don’t have the votes, you don’t shut the government down.”

“In the showdown with Nancy Pelosi, Trump’s been exposed as pitifully weak, all bluster, a pathetic negotiator,” tweeted Peter Wehner, an aide in the Reagan and both Bush White Houses. “Pelosi rolled him in every way. Egged on by right wingers, the whole thing was buffoonish from start to finish.”

Trump supporters pointed to the possibility that he still could get the wall built by declaring a national emergency, although that move likely would face an immediate legal challenge.

Congressional Republicans continued backing Trump’s call for a wall.

“Walls work. They’re a critical part of the solution for our immigration problem. We need a wall to keep our borders safe and lessen the drugs flowing into our country over our southern border,” Rep. Lloyd Smucker (R., Pa.) said in a statement.

In the Rose Garden, Trump said he did not need a wall covering all 2,000 miles of the southern border, but insisted that physical barriers were vital to national security.

“No border security plan can ever work without a physical barrier,” he said.

He also offered some of his first words of sympathy for the federal workers who have been furloughed or forced to work without pay, after days in which his top aides seemed to downplay the financial strain.

“You are fantastic people, you are incredible patriots,” Trump said. “Not only did you not complain, but in many cases you encouraged me to keep going because you care so much about our country.”

Alex Jay Berman, a union leader who works in customer service at the IRS in Philadelphia, said it was too soon to express relief for him and his more than 3,300 members.

"We don't know what will pass in the House and the Senate, we don't know when we'll be back to work, and perhaps most importantly, we don't know when paychecks will come out," he said.

Looking further ahead, he said, “What happens if three weeks go by and we start up on Day 36 of the shutdown?”

Alex Jay Berman, right; his wife, Vicki, left; and their son, Hunter, 7, stand for a portrait outside their Northeast Philadelphia home on Friday, Jan. 11, 2019. The Bermans are both IRS employees who have been furloughed as a result of the ongoing government shutdown.
TIM TAI / Staff Photographer
Alex Jay Berman, right; his wife, Vicki, left; and their son, Hunter, 7, stand for a portrait outside their Northeast Philadelphia home on Friday, Jan. 11, 2019. The Bermans are both IRS employees who have been furloughed as a result of the ongoing government shutdown.

Mindi Snoparsky, a hydrogeologist for the Environmental Protection Agency who had rallied with other federal workers at the Philadelphia airport on Friday morning, said she was glad to go back to work but also concerned that the reprieve would be temporary.

“It should be for more than three weeks, but that’s OK, I’m not going to argue,” said Snoparsky, 62. She was looking forward to going back to work, and noted that she and her colleagues hadn’t been allowed to monitor their emails or get on their work computers.

“I don’t know what I’ll do first,” she said. "Some things are way overdue. I don’t even know.”

Trump could face a backlash from his conservative supporters if he fails to deliver one of the signature promises of his campaign, one so popular with his base that the chant “Build the wall!” needs no explanation.

But the partial shutdown, the longest in U.S. history, took a political toll on Trump.

A Washington Post-ABC News poll released Friday showed that his disapproval rating had grown to 58 percent, up 5 percentage points from the last survey in November. About 63 percent of independents disapproved of Trump’s performance, up from 53 percent in November.

The delays and disruptions at airports Friday, including in Philadelphia and Newark, added another visible impact from the shutdown and appeared to provide impetus for Trump and lawmakers to end the standoff.

Marie Owens Powell, center, of Wildwood, N.J., president of the American Federation of Government Employees Local 3631 representing EPA workers, stands during a rally against the government shutdown outside Philadelphia International Airport on Friday, Jan. 25, 2019. Local members of Congress and furloughed government employees called for an end to the shutdown as workers missed another paycheck Friday.
TIM TAI / Staff Photographer
Marie Owens Powell, center, of Wildwood, N.J., president of the American Federation of Government Employees Local 3631 representing EPA workers, stands during a rally against the government shutdown outside Philadelphia International Airport on Friday, Jan. 25, 2019. Local members of Congress and furloughed government employees called for an end to the shutdown as workers missed another paycheck Friday.

Sen. Brian Schatz (D., Hawaii) said lawmakers should feel “shame” that it took such problems to prompt action, when strains on food stamps and federal workers and other shutdown fallout failed to move the ball.

“Today is an indicator of what is happening to the government, of who matters to the government,” Schatz said on the Senate floor. “If the elites are imperiled or inconvenienced in any way at all, game over. And shame on us if that’s what it takes.”

Staff writers Juliana Feliciano Reyes, Michaelle Bond, and Oona Goodin-Smith contributed to this article.