Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract time in PHP

I have been looking for an answer for a few hours now, but I can't find one.

I'm writing a simple script. The user sets their work start and end time. So, for example, somebody is working from 8:00 to 16:00. How can I subtract this time to see how long the person has been working?

I was experimenting with strtotime(); but without success...

like image 835
PsychoX Avatar asked Mar 28 '11 18:03

PsychoX


1 Answers

A bit nicer is the following:


$a = new DateTime('08:00');
$b = new DateTime('16:00');
$interval = $a->diff($b);

echo $interval->format("%H");

That will give you the difference in hours.

like image 110
fab Avatar answered Oct 13 '22 18:10

fab