为什么time()不返回与strtotime相同的值(“2013-08-28 06:38:47”)


Why does time() not return the same value as strtotime("2013-08-28 06:38:47")

我在6:38:47运行了time(),它返回的值与同一时间的strtotime不同。为什么会这样?

它应该绝对返回相同的值。运行此代码:

<?php
$time = time();
$string = date('Y-m-d H:i:s', $time);
$strtotime = strtotime($string);
print "time = $time'n";
print "string = $string'n";
print "strtotime = $strtotime'n";
print "difference = ".($time-$strtotime)."'n";
?>

我现在的输出:

time = 1377686839
string = 2013-08-28 12:47:19
strtotime = 1377686839
difference = 0

你对此有什么不同吗?你也可以发布你的测试代码,也许其中有一个错误。

strtotime根据系统时区解释时间字符串参数;time与时区无关,因为它只返回自Unix epoch开始以来的秒数。

如果您的系统时区不是UTC,您应该期望值不同,因为您传递给strtotime的时间字符串参数是硬编码的。你对"当前挂钟时间"的概念和系统的概念不同,因此时间戳也不同。