小问题PHP date/strtotime函数

Small issue PHP date / strtotime function

我的代码中有一个小问题。我正在尝试计算 2 次之间的差异。

这是我的代码:

$dif = strtotime($tt['endtime']) - strtotime($tt['starttime']);
echo date("H:i", $dif);

$tt['endtime'] = 13:00 和 $tt['starttime'] 是 12:00 所以输出应该是 01:00,但是输出是 02:00.

我做错了什么?

提前致谢!

这是因为 date() 的输出是 "altered" 按您的服务器所在的时区。您可以查看 了解更多信息。

相反,您可以使用 gmdate()(时区没有问题):

$endtime = "13:00";
$starttime = "12:00";
$difference = abs(strtotime($endtime) - strtotime($starttime));
echo gmdate("H:i", $difference);
// 01:00

https://3v4l.org/BkRFM 输出。

使用此代码进行时间减法

$tt['endtime']   = '13:00:00';
$tt['starttime'] = '12:00:00';

$dif = strtotime($tt['endtime']) - strtotime($tt['starttime']); //here you can subtraction occurs

echo gmdate("H:i", $dif);  // In this step you got the correct output 01:00

使用 gmdate()

$dif = strtotime('13:00')-strtotime('12:00');
echo gmdate("H:i", $dif);