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';

3

3 Answers 3

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.

0
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');
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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.