How to calculate the difference in time between two PHP dates

Asked

Viewed 1,484 times

0

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

The entry date "$datae" and The "$dates" departure date is intended to store an employee’s date of entry and date of exit. So far so good, now we need to get these two values "$datae" and "$dates" and make the difference to know the total working hours.

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
  • My difficulty is in getting a date already inserted, ie the part "$start = new Datetime( '2017-01-9 10:00:00' );" I am not able to get the date that was inserted in the form to replace the part "2017-01-9 10:00:00"but it is storing the date of exit and entry in the database

Browser other questions tagged

You are not signed in. Login or sign up in order to post.