PHP计算小时数

PHP计算小时数,php,Php,我有两个变量,例如: $from = 13:43:13; $to = 18:53:13; 我需要用PHP计算$from和$to之间的小时数,这样$total将如下所示: $total = 5.10 // 5 hours and ten minutes 或 我不在乎秒,我需要的是小时和分钟 请帮助我:)制作一个函数并将下面的代码放入其中如何 <?php $from = $_REQUEST['start_time']; $to = $_REQUEST['end_time

我有两个变量,例如:

$from = 13:43:13;

$to = 18:53:13;
我需要用PHP计算
$from
$to
之间的小时数,这样
$total
将如下所示:

$total = 5.10 // 5 hours and ten 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;

0.40
hours将是24分钟,而不是40.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(正确)。通过我遇到的每一个例子来发现这个问题。这里的想法是小数点前的数字应该是小时数,小数点后的数字应该是分钟数。由于时间间隔为0小时7分钟,它应该返回0.7,而不是0.07。在我看来,这不是一个特别有用的数字,但这是OP想要的。。。
$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;
$from = new DateTime('13:43:13');
$to = new DateTime('18:53:13');

echo $from->diff($to)->format('%h.%i'); // 5.10