PHP:减去时间

时间:2013-11-20 10:14:44

标签: php

这是我减去时间的代码......但这里的问题不是减去秒......

fldFullTimefldTotalDuration来自我的数据库..

假设fldFullTime 10:00:00

然后fldTotalDuration 08:30:00

$fulltime = new DateTime($row['fldFullTime']);
$totalduration = new DateTime($row['fldTotalDuration']);

$res= $fulltime ->diff($totalduration);
echo "DIFF: ".$res->format("%H:%S");

我获得此代码的结果是 01:00

因此,当我将fldFullTime [10:00:00]减去fldTotalDuration [08:30:00]时,结果应该是 1:30:00

我该怎么做?

感谢您的帮助

5 个答案:

答案 0 :(得分:4)

您使用的是错误的格式字符串:

  • %H:小时
  • %S:秒

您需要添加分钟:

$fulltime = new DateTime('10:00:00');
$totalduration = new DateTime('08:30:00');
$res= $fulltime ->diff($totalduration);

echo "DIFF: ".$res->format("%H:%S");
//DIFF: 01:00
echo "DIFF: ".$res->format("%H:%I:%S");
//DIFF: 01:30:00

答案 1 :(得分:3)

检查一下:

  $fulltime = new DateTime('10:00:00');
$totalduration = new DateTime('08:30:00');

$res= $fulltime ->diff($totalduration);
echo "DIFF: ".$res->format("%H:%I:%S");

答案 2 :(得分:3)

在您的情况下,格式应为%H:%I:%S

请参阅DateInterval::format文档。

答案 3 :(得分:3)

你错过了'我',它显示了分钟。

$fulltime = new DateTime('10:00:00');
$totalduration = new DateTime('08:30:00');

$res= $fulltime->diff($totalduration);
echo "DIFF: ".$res->format("%H:%i:%S");

答案 4 :(得分:-1)

我使用explode完成了它:

$fulltime = new DateTime($row['fldFullTime']);
$totalduration = new DateTime($row['fldTotalDuration']);

$piece1 = explode(":", $fulltime);
$piece2 = explode(":", $totalduration);

$hours=$piece2[0]-$piece1[0];
$min=$piece2[1]-$piece1[1];
$sec=$piece2[2]-$piece1[2];

Echo "$hours:$min:$sec";