Issue in getting the time difference between two variables

I have two textboxes that inputted time, and I want to get the difference between two time.

for example:

$IN = 13:35;
$OUT = 17:35;

$OTHours = ($OUT - $IN);

$OTHours = 4.00;

and it is correct, but I have a scenario like this:

$IN = 21:35;
$OUT = 05:35;

$OTHours = -16.00;

it should be 8.00.

Any help is highly appreciated.

Thank you…

Try this:



$IN          = '21:35';
$OUT       = '05::35';
$OUT       = ($IN > $OUT) ? ('24:00' + '05:35') : $OUT;
$OThours = ($OUT - $IN);


Above script works in PHP. What programming language are you using?

I have never used $OThours, new to me - learn something everyday :slight_smile:

//

When i tried this the 05:35 becoome 29…

What if i add date in time?it should be much better?

Than you

Until others give another solid way, try to understand this:


$in = '21:35';
$out = '05:35';
// check if the out time is less than the in time
if(strtotime($out) < strtotime($in)){
	$in = date('Y-m-d') . ' 21:35'; // append the in time with today's date
	$out = date('Y-m-d', strtotime("tomorrow")) . ' 05:35'; // append date out time with next day/tomorrow
	$diff = strtotime($out) - strtotime($in);
}
else{
	$diff = strtotime($out) - strtotime($in);
}
echo round(($diff / 60) / 60);

Is it something you are looking for?

This is tricky :smiley:

Best way is probably strtotime as mentioned before. However you must take into consideration that an $in time of 23:40 and an $out time of 02:30 the $out time is on the next day so…

$in = '23:40';
$out = '02:30';

$inTimestamp = strtotime($in);
$outTimestamp = strtotime($out);
if((substr($out, 0, 1) == 0) && substr($in, 0, 1) != 0){
    $outTimestamp = strtotime("tomorrow", $outTimestamp);
}

$diff = $outTimestamp - $inTimestamp;
echo round($diff / 360);

Something like this should work…

I have this new code:


$from_time  = strtotime($IN);
$to_time = strtotime($OUT);
$OTHours = round(abs($to_time - $from_time) / 3600);

$IN = 2011-12-22 13:30;
$OUT = 2011-12-22 15:30;

from this code the $OTHours result is 1.00
it should be 1.30

by the way the OTHours field is Decimal Type.

round() makes a round number so thats why you get 1.00 instead of 1.30. If the true value was 1.60 you would get 2.00 you have to provide a “precision” to fix this:

$OTHours = round(abs($to_time - $from_time) / 3600, 2);

Try this:


function index()
{ 

$in       = '19:30'; 
$secs_in  = strtotime( $in );
$times    = array
(
  '19:30', 
  '19:31', 
  '23:35', 
  '01:35', 
  '03:35', 
  '05:35', 
  '07:35', 
  '19:29', 
);
echo "
  <!doctype html>
    <head>
      <title>Time difference</title>
      <style type='text/css'>
        body  {text-align:center; font-size:14px; line-height:1.8em}
        table {background-color:#cfc; margin:2em auto}
        th {width:10em}
      </style>  
    </head>
  <body>  
";
echo "<table>
        <tr style='font-weight:700; color:#f00'>
          <th> time in    </th>
          <th> time out   </th>
          <th> secs_in    </th>
          <th> secs_out   </th>
          <th> secs_diff  </th>
          <th> mins_diff  </th>
          <th> hour:min   </th>
        </tr>
      ";

foreach ( $times as $out ):
  # maybe $out time is less than $in time
  $secs_out  = strtotime( $out );   //echo '<br />$out ---> ' .$out .' == ' .$secs_out  .' seconds';  
  if ( $secs_out < $secs_in )
  {
    $secs_out = 24 * 60 * 60 + $secs_out; // add full day of seconds to time in
  }
  
  $secs_diff  = $secs_out - $secs_in;
  $mins_diff  = sprintf('%d',        $secs_diff / 60);
  $duration   = sprintf('%02d:%02d', $mins_diff/60, $mins_diff % 60);
  
  echo '<tr>';
    echo '<td>' .$in                       .'</td>';
    echo '<td>' .$out                      .'</td>';
    echo '<td>' .number_format($secs_in)   .'</td>';
    echo '<td>' .number_format($secs_out)  .'</td>';
    echo '<td>' .number_format($secs_diff) .'</td>';
    echo '<td>' .$mins_diff                .'</td>';
    echo '<td>' .$duration                 .'</td>';
  echo '</tr>';
endforeach;
echo '</table><body></html>';
die;


Output:


time in 	time out 	secs_in 	secs_out 	secs_diff 	mins_diff 	hour:min
19:30	19:30	1,328,553,000	1,328,553,000	0	0	00:00
19:30	19:31	1,328,553,000	1,328,553,060	60	1	00:01
19:30	23:35	1,328,553,000	1,328,567,700	14,700	245	04:05
19:30	01:35	1,328,553,000	1,328,574,900	21,900	365	06:05
19:30	03:35	1,328,553,000	1,328,582,100	29,100	485	08:05
19:30	05:35	1,328,553,000	1,328,589,300	36,300	605	10:05
19:30	07:35	1,328,553,000	1,328,596,500	43,500	725	12:05
19:30	19:29	1,328,553,000	1,328,639,340	86,340	1439	23:59