我有两个变量,例如:PHP计算时间
$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
我不介意秒,我需要的是小时和分钟。
请帮我:)
我有两个变量,例如:PHP计算时间
$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
我不介意秒,我需要的是小时和分钟。
请帮我:)
如何作出有关的函数,并把下面的代码到你?
<?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";
}
?>
请添加字段接收值...
$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;
也可以根据你希望它的行为来设置分钟... – 2012-02-01 13:12:44
09:19 - 09:12 = 0.07。不是上面代码给出的0.7。 09:49 - 09:34 = 0.15(正确)。所以反过来,加0.7 + 0.15 = 0.85(不正确)。应该是0.07 + 0.15 = 0.22(正确)。用我遇到的每个例子来查找这个问题。 – 2015-06-11 21:23:08
这里的想法是小数点前的数字应该是小时数,而后数应该是分钟数。由于你的时间之间有0小时7分钟,它应该返回0.7,而不是0.07。在我看来,这不是一个特别有用的数字,但这是OP想要的...... – 2015-06-12 10:39:31
我喜欢使用面向对象的方法,使用DateTime类:
$from = new DateTime('13:43:13');
$to = new DateTime('18:53:13');
echo $from->diff($to)->format('%h.%i'); // 5.10
'0.40'小时会是24分钟,而不是40. – mario 2012-02-01 12:52:27