0

I am trying to subtracting two variables in PHP (time format, "H:i:s"), i.ex:

$entryscan = '07:15:00';
$exitscan = '16:35:00';

How do I get the work duration? I mean $workduration = $exitscan - $entryscan; so, the answer is $workduration = '09:20:00';

yoelpc4
  • 29
  • 1
  • 6
  • 1
    Possible duplicate of [Subtract time in PHP](http://stackoverflow.com/questions/5463549/subtract-time-in-php) – Rakesh Sojitra Oct 22 '16 at 04:55
  • 3
    Possible duplicate of [Subtracting two dates in php](http://stackoverflow.com/questions/10469037/subtracting-two-dates-in-php) – Peter VARGA Oct 22 '16 at 04:55
  • 1
    Possible duplicate of [Get total time difference between two dates using php](http://stackoverflow.com/questions/10696654/get-total-time-difference-between-two-dates-using-php) – Dave Oct 22 '16 at 05:01

3 Answers3

1

Use php timediff function.

$datetime1 = new DateTime($entryscan);
$datetime2 = new DateTime($exitscan);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%H hours');

%H will give you the difference in hours.

%I will give you the difference in minutes.

%S will give you the difference in seconds.

Arshid KV
  • 9,631
  • 3
  • 35
  • 36
1

Try to use php DateTime Class if your php version >= 5.2

$entryscan = '07:15:00';
$exitscan = '16:35:00';

$entryTime = new DateTime($entryscan);
$exitTime = new DateTime($exitscan);
$interval = $entryTime->diff($exitTime);
$workduration = $interval->format('%H:%I:%S');
Abu Sayem
  • 1,170
  • 6
  • 13
0
$entryscan = '07:15:00';
$exitscan = '16:35:00';
$time1 = new DateTime($entryscan);
$time2 = new DateTime($exitscan);
$diffis = $time1->diff($time2);
echo $workduration = $diffis->format('%H:%I:%S');

@Arshid i just edit your code is working fine.

Naveen Kumar
  • 481
  • 5
  • 16