PHP中两个时间戳的区别

时间:2015-05-25 02:09:04

标签: php time mamp strtotime maxlength

在我看来,这应该回响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小时。

3 个答案:

答案 0 :(得分:3)

问题在于:

service

请注意日期如何从更改时区转移一天?这是因为您提供给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 的日期是相对日期,因此您需要" ground"它反对一天的开始:

strtotime()

或者,使用DateTime

echo date('H:i', strtotime('17:00') - strtotime('06:00') + strtotime('00:00'));

答案 1 :(得分:1)

因为它将更改为本地服务器php时间。

试试这个

<?php

    $start_time = "06:30";
    $end_time = "17:50";
    $default_time = "00:00";


    $LOD = date("H:i", (strtotime($default_time)+(strtotime($end_time) - strtotime($start_time))));
    echo  "Length of day: " . $LOD . " hours";

    echo "<br>";

    $diff = strtotime($end_time) - strtotime($start_time);
    $hour = floor($diff / 3600);
    $minute = ($diff % 3600) / 60;
    echo "Length of day: " . $hour . ":" . $minute . " hours";
?>

答案 2 :(得分:0)

以这种方式使用date没有任何意义。

如果您想要最低感,那么您应该添加strtotime("00:00")作为初始时间。

(end - start) + init

相关问题