0

How do I count the hours and mins between the 2 time, like for example:

between 9:00 and 6:20

I want to count the time from in to out. what should be retrieve is 9:20.

I am using codeigniter for these;

$in = date('h:i',strtotime("09:00:00"));
$out = date('h:i',strtotime("06:20:00"));
VinceGraphic
  • 85
  • 1
  • 9

4 Answers4

2
echo dateDifference('09:00:00','06:20:00');


function dateDifference($date_1 , $date_2 , $differenceFormat = '%h %i' )
{
    $datetime1 = date_create($date_1);
    $datetime2 = date_create($date_2);

    $interval = date_diff($datetime1, $datetime2);

    return $interval->format($differenceFormat);

}
Gayan
  • 2,845
  • 7
  • 33
  • 60
0

This might help

$to_time = strtotime("2008-12-13 10:42:00");
$from_time = strtotime("2008-12-13 10:21:00");
echo round(abs($to_time - $from_time) / 60,2). " minute";

from here

How to get time difference in minutes in PHP

Community
  • 1
  • 1
Royts
  • 501
  • 6
  • 14
0
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
0

In this problem first you need to convert 6.20 to 18:20. Use the code below

$in = '9:00';
$out = '6:20';
$new_out=date('H:i:s',strtotime($out.'+ 12 hours'));

Now, the new value of the out time is 18:20.

To find the difference between two times, use the following code

$formated_in = new DateTime($in);
$formated_new_out = new DateTime($new_out);
$dteDiff  = $formated_in->diff($formated_new_out);
echo $dteDiff->format("%H:%I");

The output is 09:20

Thanks

BlueCacti
  • 9,729
  • 3
  • 20
  • 24
Akbor
  • 1,280
  • 1
  • 9
  • 11