在我看来,这应该回显 11 小时,但它返回 12;我错过了什么?
$start_time = "06:00";
$end_time = "17:00";
$LOD = date("H:i", ((strtotime($end_time) - strtotime($start_time))));
echo "Length of day: " . $LOD . " hours";
更新:在 MAMP 环境中在我的 MBPro 上运行;系统时间设置为 24 小时制。
最佳答案
问题出在这里:
date_default_timezone_set('Asia/Singapore');
echo date('r', strtotime('06:00')); // Mon, 25 May 2015 06:00:00 +0800
date_default_timezone_set('America/Denver');
echo date('r', strtotime('06:00')); // Sun, 24 May 2015 06:00:00 -0600
注意到日期如何因更改时区而移动了一天吗?这是因为您提供给 strtotime()
的日期是相对日期,因此您需要根据一天的开始“基础”它:
echo date('H:i', strtotime('17:00') - strtotime('06:00') + strtotime('00:00'));
或者,使用DateTime :
$t1 = new DateTime('06:00');
$t2 = new DateTime('17:00');
echo $t2->diff($t1)->format('%H:%I');
关于php - PHP 中两个时间戳之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30430354/