我有两个变量,例如:
$from = 13:43:13;
$to = 18:53:13;
我需要在PHP中计算$from
和$to
之间的小时数,使得$total
变成:
$total = 5.10 // 5 hours and ten minutes
或者
$total = 0.40 // 0 hours and 40 minutes
我不介意秒数,我需要的是小时和分钟。 请帮帮我 :)
我有两个变量,例如:
$from = 13:43:13;
$to = 18:53:13;
我需要在PHP中计算$from
和$to
之间的小时数,使得$total
变成:
$total = 5.10 // 5 hours and ten minutes
或者
$total = 0.40 // 0 hours and 40 minutes
$from = '13:43:13';
$to = '18:53:13';
$total = strtotime($to) - strtotime($from);
$hours = floor($total / 60 / 60);
$minutes = round(($total - ($hours * 60 * 60)) / 60);
echo $hours.'.'.$minutes;
我喜欢使用面向对象的方法,使用DateTime类:
$from = new DateTime('13:43:13');
$to = new DateTime('18:53:13');
echo $from->diff($to)->format('%h.%i'); // 5.10
<?php
$from = $_REQUEST['start_time'];
$to = $_REQUEST['end_time'];
$action = $_REQUEST['action'];
?>
<?
if($action && ($action == "go")){
list($hours, $minutes) = split(':', $from);
$startTimestamp = mktime($hours, $minutes);
list($hours, $minutes) = split(':', $to);
$endTimestamp = mktime($hours, $minutes);
$seconds = $endTimestamp - $startTimestamp;
$minutes = ($seconds / 60) % 60;
$hours = round($seconds / (60 * 60));
echo "Time passed: <b>$hours</b> hours and <b>$minutes</b> minutes";
}
?>
计算中存在缺陷。结果显示为5.2,但应该是5.02。
尝试使用09:58作为开始时间,15:00作为结束时间。