How to calculate the difference in hours between two dates PHP [duplicate]

0

I have two dates in the format yyyy-mm-dd HH: ii: ss

The entry date "$ dataE" and the exit date "$ dataS", is intended to save an employee's date of entry and exit date. So far so good, now we need to get those two values "$ dataE" and "$ dataS" and make the difference to know the total working hours.

    
asked by anonymous 24.02.2018 / 16:20

1 answer

1
    $start  = new \DateTime( '2017-01-9 10:00:00' );
    $end    = new \DateTime( '2018-02-10 11:39:37' );

    $interval = $start->diff( $end );

    echo implode(",", [
        $interval->y . " anos",
        $interval->m . " meses",
        $interval->d . " dias",
        $interval->h . " horas",
        $interval->i . " minutos",
        $interval->s . " segundos",
    ]);

    echo "Diferença em Horas é : " . ($interval->h + ($interval->days * 24));

Will display:

    1 anos,1 meses,1 dias,1 horas,39 minutos,37 segundos
    Diferença em Horas é : 9529
    
24.02.2018 / 16:48