By Chuck Mikolajczak and David Morgan

NEW YORK/WASHINGTON (Reuters) - Donald Trump's fellow Republicans rebuked the president on Wednesday after his threat to shut down the U.S. government over funding for a border wall rattled markets and cast a shadow over congressional efforts to raise the country's debt ceiling and pass spending bills.

Congress will have about 12 working days when it returns on Sept. 5 from its summer break to approve spending measures to keep the government from shutting down, and a deadline also is closing in for raising the cap on the amount the federal government may borrow.

With those deadlines looming in late September and early October, Trump raised the prospect in a speech on Tuesday evening of a shutdown if Congress does not agree to fund his long-promised wall along the border with Mexico.

U.S. stocks and the dollar weakened and investors pivoted to the safety of U.S. Treasury securities on Wednesday. The S&P 500 Index (.SPX) was about 0.3 percent lower in afternoon trading. The Dow Jones Industrial Average (.DJI) was down by 0.3 percent and the Nasdaq Composite Index (.IXIC) slid 0.2 percent. The dollar weakened against both the euro and yen.

Trump made building a border wall to deter illegal immigration a central part of his 2016 election campaign but the issue of funding it has not gained traction as lawmakers, including many Republicans, question whether it is necessary.

The top Republican in Congress, House of Representatives Speaker Paul Ryan, said on Wednesday that a wall was necessary but the government did not have to choose between border security and a government shutdown.

"I don't think anyone's interested in having a shutdown," he told reporters in Hillsboro, Oregon, where visited an Intel factory. "I don't think it's in our interest to do so."

Representative Tom Cole of Oklahoma, a Republican who chairs a House Appropriations subcommittee, said shutting down the government was very "unwise" and such a move could backfire on the party that controls power in Washington.

Story continues

“When you control the presidency, the Senate and the House, you’re shutting down the government that you’re running. I don’t think it’s smart politically and I don’t think it would succeed practically,” he told Reuters in an interview.

For more news videos visit Yahoo View, available on iOS and Android.

Republicans have been trying for months to make a deal on funding the government. Congress regularly faces this problem and usually ends up passing a temporary bill extending funding levels unchanged for a few weeks or months. Sometimes that sort of agreement cannot be reached, often because of an uncompromising stand taken on a narrow issue, and the federal government shuts down for a few days.

Ryan suggested there would be a need for a short-term measure, or continuing resolution, saying the Senate would probably move slower than the House on passing a spending bill.

This would push the budget battle to later in the year and could in turn delay congressional efforts to approve tax reform, another signature campaign issue of Trump's.

The friction over wall funding is just the latest brush between Trump and congressional Republicans. He has castigated some leading Republicans, notably Senate Majority Leader Mitch McConnell, and has been publicly infuriated by the fact no significant legislation has been passed since he took office in January.

'RAN ON IT, WON ON IT'

The White House stressed on Wednesday that Trump planned to work with Congress to get funding for the wall. "The president ran on it, won on it and plans to build it," said White House spokeswoman Natalie Strom.

Trump's insistence did garner some support.

Representative Jim Jordan of Ohio, a founding member of the conservative Freedom Caucus, told Reuters he strongly backed the president's call for wall funding and said any government shutdown would be due to Senate Democratic leader Chuck Schumer and other Democrats.

While campaigning, Trump said Mexico should pay for the wall, but Mexico has always resisted this and since taking office Trump has said that the United States will fund it initially and be repaid by Mexico.

Critics said Trump's shutdown threat undermined confidence in the United States.

"Trump saying he would be willing to shut down the government over the wall obviously doesn’t really inspire much confidence in anyone," said Michael O'Rourke, chief market strategist at JonesTrading in Greenwich, Connecticut.

He said even if this was just a negotiating tactic, most people in the United States and in financial markets did not think wall funding was an issue worth shutting the government down over.

Representative Adam Kinzinger, a Republican member of the House Foreign Affairs Committee, said the threat was "dangerous for the confidence of the markets and it's dangerous for our role in the world as we're talking to nations like Afghanistan to say, 'Here's how you govern yourself.'"

The House passed a spending bill late last month that included funding for the wall. The Republicans' slim majority means Democrats are needed to pass most legislation and they have opposed including border wall funding in any fiscal 2018 spending bill.

DEBT LIMIT

Related to the budget is the debt limit. Congress periodically raises it to allow more borrowing by the U.S. government but politicians sometimes take advantage of the need for this to insist on changes in federal spending or other priorities.

The Treasury Department, already using "extraordinary measures" to remain current on the federal government's obligations, has said the debt limit will need to be raised by Sept. 29.

The Trump administration has asked Congress to extend the debt limit with a "clean" bill that excludes any other provisions, something that Republicans who seek conditions to rein in spending may well balk at.

Credit ratings agency Fitch said on Wednesday that if the debt limit is not raised in a timely manner, it would review the U.S. sovereign debt rating, "with potentially negative implications." The rating is its measure of confidence in the soundness of the U.S. economy.

U.S. stocks, which were boosted by Trump's election in November and his promises of a business-friendly agenda that includes tax cuts, have stumbled in recent weeks as a result of turmoil in Washington, with the Trump administration repeatedly distracted by controversies and tensions with North Korea.

The most recent U.S. government shutdown was from Oct. 1-16, 2013, when hundreds of thousands of non-essential federal workers were furloughed in a dispute over funding for Obama's healthcare law.

(Reporting by Chuck Mikolajczak in New York and David Morgan in Washington; Additional reporting by Karen Brettell, Dion Rabouin, Sinead Carew, Sam Forgione and Richard Leong in New York, Doina Chiacu, Tim Ahmann, David Morgan, Susan Cornwell, Kevin Drawbaugh, Amanda Becker and Steve Holland in Washington; Writing by Dan Burns and Frances Kerry; Editing by Bill Trott)