我有两个变量,例如:
$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
我不介意秒,我需要的是小时和分钟。
请帮帮我:)。
答案 0 :(得分:5)
$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;
答案 1 :(得分:2)
我喜欢使用面向对象的方法,使用DateTime类:
$from = new DateTime('13:43:13');
$to = new DateTime('18:53:13');
echo $from->diff($to)->format('%h.%i'); // 5.10
答案 2 :(得分:0)
如何制作一个函数并将下面的代码放入其中?
<?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";
}
?>
请添加字段以接收值...