PHP中两个时间戳的区别

It seems to me that this should echo 11 hours but it returns 12; what am I missing?

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

Update: running on my MBPro in a MAMP environment; system time set to 24 hr.

Using date that way doesn't make any sense.

If you want a minimum sense, then you should add strtotime("00:00") as your initial time.

(end - start) + init

Because it will change to your local server php time.

Try this

<?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";
?>

The problem lies here:

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

Notice how the date has shifted by one day from changing the timezone? This is because the date you supply to strtotime() are relative dates, so you need to "ground" it against the start of day:

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

Or, use DateTime:

$t1 = new DateTime('06:00');
$t2 = new DateTime('17:00');
echo $t2->diff($t1)->format('%H:%I');