The US Senate has passed a bill calling for all US combat troops to leave Iraq within a year, defying a veto threat.
US troops in Iraq: Coming home soon?
The Senate vote came only an hour after George W Bush made his third vow in less than a week to veto such a law.
"I'll veto a bill that restricts our commanders on the ground... a bill that doesn't fund our troops," he said.
The vote of 51 to 47 links funding for the war to a goal of getting US combat troops out of Iraq by March 2008. The House has passed a similar bill.
A BBC correspondent in Washington says it is one of the boldest challenges yet to the Bush administration's handling of the war in Iraq.
The Senate bill approves $122bn (£62bn) in funds - mostly for the wars in Afghanistan and Iraq - but also orders the president to begin withdrawing troops from Iraq within 120 days of passage of the bill.
It sets a goal of pulling all combat troops out of Iraq by the end of March of next year, but does not explicitly demand their removal.
The House and Senate must now reconcile their two versions of the bill and send the result to Mr Bush.
If he refuses to sign it and returns it unsigned, the bill will not become law.
The votes in both the House and Senate were close enough to suggest Congress will not be able to override his threatened veto.
Challenge to Bush
But the fact both houses were able to approve deadlines for bringing the troops home constitutes the strongest challenge Congress has mounted to Mr Bush's war policy.
President Bush has made clear he will not let the bill become law
President Bush's Republican party lost control of both houses of Congress to the Democrats last year.
The Senate vote came a day after the leaders of both houses wrote to the president placing the blame on him for the expected impasse over the bill.
"We stand ready to work with you, but your threats to veto a bill that has not even been presented to you indicate that you may not be ready to work with us," House leader Nancy Pelosi and Senate leader Harry Reid wrote.
The Senate had earlier attempted to pass a nonbinding resolution calling for the troops to come home, but Republicans prevented the measure from coming to a vote.
They did not block Thursday's funding bill, Republican leaders said, because they did not want to deprive the troops of essential supplies.
The bill passed mostly along party lines.