从字符串到毫秒并返回

时间:2014-02-10 15:04:22

标签: php time

我想在vars中存储两个小时的操作。我首先转换为毫秒进行操作,然后转换回可读的东西。它似乎工作,但最终的数字是不对的。在这个例子中它给了我1:30:00的差异。有人可以解释原因吗?

// I have two hours that come from a MySql database and I store in a var:
$start= "16:00:00";
$end= "16:30:00";

// I convert to milliseconds and rest:
$startMili = strtotime($start);
$endMili = strtotime($end);
$dif = $endMili - $startMili ;

// convert to something readable:
echo "<br><br> diference: ";
$readable = date("H:i:s", $dif);
echo $readable;

1 个答案:

答案 0 :(得分:1)

您也可以使用DateTime而不是strtotime

见下面的例子

$datetime1 = new DateTime($start, new DateTimeZone('Europe/Amsterdam'));
$datetime2 = new DateTime($end, new DateTimeZone('Europe/Amsterdam'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%H:%i:%s');