改用DateTime试试。
这需要一些解决方法,因为DateInterval
(由 返回DateTime::diff()
)不计算微秒,因此您需要手动执行此操作
$pageTime = new DateTime("2012-04-23T16:08:14.1 - 5 hours");
$rowTime = new DateTime("2012-04-23T16:08:16.9 - 5 hours");
// the difference through one million to get micro seconds
$uDiff = abs($pageTime->format('u')-$rowTime->format('u')) / (1000 * 1000);
$diff = $pageTime->diff($rowTime);
echo $diff->format('%s')-$uDiff;
我总是推荐DateTime
因为它的灵活性,你应该研究一下
编辑
为了向后兼容 PHP 5.2,它采用与毫秒相同的方法:
$pageTime = new DateTime("2012-04-23T16:08:14.1 - 5 hours");
$rowTime = new DateTime("2012-04-23T16:08:16.9 - 5 hours");
// the difference through one million to get micro seconds
$uDiff = abs($pageTime->format('u')-$rowTime->format('u')) / (1000 * 1000);
$pageTimeSeconds = $pageTime->format('s');
$rowTimeSeconds = $rowTime->format('s');
if ($pageTimeSeconds + $rowTimeSeconds > 60) {
$sDiff = ($rowTimeSeconds + $pageTimeSeconds)-60;
} else {
$sDiff = $pageTimeSeconds - $rowTimeSeconds;
}
if ($sDiff < 0) {
echo abs($sDiff) + $uDiff;
} else {
// for the edge(?) case if $dt2 was smaller than $dt
echo abs($sDiff - $uDiff);
}