David Cameron has warned Israel that it is "wrong and illegal" to target civilians, in his strongest comments so far on the conflict in Gaza which has killed more than 1,800 people.

The prime minister would not be drawn on whether he believes Israel has broken international law by shelling children outside a school, but he told the BBC the UN was "right to speak out in the way it has, because international law is very clear that there mustn't be the targeting of civilians or the targeting of schools, if that's what's happened".

It comes after the US said on Friday that the missile strike on a school was "totally unacceptable", while the UN said on Sunday a subsequent attack was a "moral outrage" and a criminal act.

Cameron addressed the issue after days of pressure from the opposition leader, Ed Miliband, to condemn Israel's military action in Gaza.

The prime minister does not go as far as the Labour leader, who has said the whole incursion into Gaza is wrong, or Nick Clegg, who has said Israel's actions appear to be a disproportionate act of collective punishment and called for talks with Hamas.

However, Cameron's comments are a significant hardening of the UK position after the third report of shelling in or outside a UN school in recent days.

Speaking before commemorations for the centenary of the first world war, Cameron said: "International law is very, very clear that the use of force always has to be proportionate, that civilians should not be targeted, and I was clear about that two weeks ago in the House of Commons."

The prime minister defended himself against Miliband's criticism that the UK had not been bold or quick enough to criticise Israel.

"We've been very clear that there needs to be an immediate, comprehensive humanitarian ceasefire and that we want this conflict to stop. And we obviously think it's appalling, the loss of life that there's been. From the start, though, we've also made the point that if the Hamas rocket attacks on Israel stop, then that would be probably the fastest way to stop this conflict," he said.

On Sunday, Miliband said politicians needed to be "speaking out without timidity and truthfully about what is happening" and warned Israel that its actions were counterproductive because every death of an innocent Palestinian could increase support for Hamas.

He said it was essential for the government to be much clearer that Israel's actions in Gaza are wrong, just like the rocket attacks perpetrated by Hamas.

"Speaking out is necessary to put the pressure on Israel as, of course, there must also be pressure on Hamas, a terrorist organisation, to end this violence."

Miliband added: "I speak as a friend of Israel. I have relatives in Israel; I care deeply about the state of Israel. But day by day as we see these outrages on our television screens, Israel is losing friends in the international community."

Downing Street said it was shocked that Miliband would try to "play politics" over the issue, while Chris Grayling, the justice secretary, suggested that Miliband's comments could be undermining the efforts to get both sides to reach a ceasefire.

"I don't think it helps if we make strong comments that are going to put us in less of a position to do that," Grayling told Sky News. "We all want to see an end to this conflict. We want to see a proper ceasefire. We want to see an end to military action on both sides. And we want to see long-term peace in the region.

"The best approach that the British government can take, in my view, is to try and work to get both sides to take that approach and not to get involved in being ultra-critical of one side or the other."