Reputation: 45757
I have two variables, example:
$from = 13:43:13;
$to = 18:53:13;
I need to calculate in PHP the hours between $from
and $to
, so that $total
will be like:
$total = 5.10 // 5 hours and ten minutes
or
$total = 0.40 // 0 hours and 40 minutes
I do not mind seconds, what I need is hours and minutes.
Please help me :)
Upvotes: 3
Views: 15579
Reputation: 1
There's a flaw in the calculation. The result shows 5.2 when it should be 5.02.
Try using 09:58 as the start time and 15:00 as the end time.
Upvotes: 0
Reputation: 28929
I like using the object-oriented approach, using the DateTime class:
$from = new DateTime('13:43:13');
$to = new DateTime('18:53:13');
echo $from->diff($to)->format('%h.%i'); // 5.10
Upvotes: 4
Reputation: 1693
$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;
Upvotes: 5
Reputation: 1861
How about making about a function and putting the below code into that?
<?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";
}
?>
Please add the fields to receive the values...
Upvotes: 0