我的代码有一个小问题。我试图计算两次之间的差异。小问题PHP日期/ strtotime函数
这是我的代码:
$dif = strtotime($tt['endtime']) - strtotime($tt['starttime']);
echo date("H:i", $dif);
$ TT [ '结束时间'] = 13:00和$ TT [ '开始时间']为12:00,以便在输出应该是01:00,但输出是02:00。
我在做什么错?
在此先感谢!
我的代码有一个小问题。我试图计算两次之间的差异。小问题PHP日期/ strtotime函数
这是我的代码:
$dif = strtotime($tt['endtime']) - strtotime($tt['starttime']);
echo date("H:i", $dif);
$ TT [ '结束时间'] = 13:00和$ TT [ '开始时间']为12:00,以便在输出应该是01:00,但输出是02:00。
我在做什么错?
在此先感谢!
这是因为date()
的输出是由您的服务器所处的时区“改变”的。有关更多信息,请参阅this question。
相反,你可以使用gmdate()
为(没有问题与时区):
$endtime = "13:00";
$starttime = "12:00";
$difference = abs(strtotime($endtime) - strtotime($starttime));
echo gmdate("H:i", $difference);
// 01:00
使用此代码对时间的减法运算
$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);
'date'预计Unix时间戳,而不是差2 –
之间正在尝试的时间差转换为日期。 5月4日11:00和5月5日11:00之后会发生什么? –
你想如何显示格式,因为你现在正在做的是通过差异是错误的,你需要手动转换这些 –