2

我有两个 24 小时格式的日期

$fulldate_hour_start    = "2013-11-11 18:16:00 ";
$fulldate_hour_end      = "2013-11-11 23:30:00 ";

我用这个减去了它们

$Hours = number_format((((strtotime($fulldate_hour_end) - strtotime($fulldate_hour_start)) / 60) / 60),2);

结果是 5 小时 23 分钟。应该是 5 小时 14 分钟。我的错误是什么?

4

3 回答 3

4

Try this :

$date1 = new DateTime("2013-11-11 18:16:00");
$date2 = new DateTime("2013-11-11 23:30:00");
$interval = $date1->diff($date2);
echo "difference " . $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds "; 
于 2013-11-11T05:44:40.000 回答
0

You're dividing by 3600 to convert from seconds to hours and then using number_format to render the result with two decimal places.

The result of the expression was:

5.23

Which is indeed approximately 5 hours and 14 minutes. (Remember that an hour has 60 minutes.)

Rather than do this, you should keep the value in seconds, and then use a function to convert it to a human readable format. One possibility is date_diff; the linked SO question has many others.

于 2013-11-11T05:41:59.647 回答
0

Since an hour consist of 60, not 100, minutes, 5.23 hours is 5 hours and 13.8 minutes. The 0.2 hour mismatch is due to rounding the real value to two decimals to get 0.23.

0.23 * 60 = 13.8
于 2013-11-11T05:42:15.990 回答