Pergunta

im calculating the hours worked by an employee and need to know if the employee have extra hours worked or if the employee doný work the total hours corresponding with his profile.

For example:

Day From_Hour To_Hour Hs_Worked Extra_time PHP_Return

 1    11:00   19:30   08:30       00:00     00:00
 2    11:30   19:30   08:00      -00:30     23:00
 3    11:00   19:45   08:45      -00:15     23:45
 4    11:00   19:55   08:55      +00:10     00:10

I have all the values, but need to calulate the Extra_time and i no have problem with positive values, but when i am trying to substract more time of that i have, php returns an equivalent to a previous day.

How can i add and substract times having only possitive or negative times (w/out previous days hours)?

Foi útil?

Solução

$start = DateTime::createFromFormat('H:i', '11:30');
$start->add(new DateInterval('PT8H30M'));
$end   = DateTime::createFromFormat('H:i', '19:30');
$diff = $start->diff($end);
echo $diff->format('%r%H:%I');

Demo

This just adds 8 1/2 hours to the start time and then gets the difference from the end time.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top