我試圖得到以毫秒爲單位的時間差。PHP的奇怪行爲DateTime類
$_SESSION['startTime'] = time();
$to_time = time();
//I call the code from here after a delay, say 4 seconds
$from_time = $_SESSION['startTime'];
$d1 = new DateTime($from_time);
$d2 = new DateTime($to_time);
print_r($d1->diff($d2));
我打印結果4秒鐘後,結果是有點像這樣:
DateInterval Object
(
[y] => 4 //---- Problem, this value should be +
[m] => 0 // |
[d] => 0 // |
[h] => 0 // |
[i] => 0 // |
[s] => 0 //<-here-----------------------------+
[invert] => 1
[days] => 1461
)
[S]應該已經4.爲什麼4是一年中的部分? 我在做什麼錯?
UPDATE - 解決
$to_time = (microtime(true));
$from_time = ($_SESSION['startTime']);
$diff = $to_time - $from_time;
print $diff;
打印
3.xxxxxx
如果你已經有2個UNIX時間戳,爲什麼不直接相減?另外,如果你正在尋找毫秒精度,你需要microtime()。 – Corbin 2012-03-11 09:54:12