我有两个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分钟。我的错误是什么?
发布于 2013-11-11 13:44:40
试试这个:
$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-11 13:42:00
您将除以3600以将秒转换为小时,然后使用number_format呈现带有两个小数位的结果。
表达式的结果是:
5.23这确实大约是5小时14分钟。(记住,一个小时有60分钟。)
而不是这样做,您应该以秒为单位保留该值,然后使用function to convert it to a human readable format。一种可能性是date_diff;关联的SO问题还有许多其他的可能性。
发布于 2013-11-11 13:42:16
因为一个小时由60分钟组成,而不是100分钟,5.23小时就是5小时13.8分钟。0.2小时的不匹配是由于将实际值四舍五入到两个小数以得到0.23。
0.23 * 60 = 13.8https://stackoverflow.com/questions/19899519
复制相似问题