简体   繁体   中英

Calculate number of time between Time In and Time Out using php

I have Time In and Time out of the users daily.

Example data:

Time In: 7:00 AM (emp_in)

Time out: 5:00 PM (emp_out)

What I am trying to do is just count the number of time between 8:00 AM to 5:00 PM since the user only scheduled for 8:00am to 5:00pm Only

Here's the sample code that I am playing with.

$result_in_out = strtotime($emp_in) - strtotime($emp_out);
$result_hours = abs(floor($result_in_out/3600));
$Considered_Hours = $result_hours;

I still can't find and formulate a good logic to interpret my desired output programmatically thanks

At first, you should find the difference between Time IN entered and 8:00 am and then add the difference time to the Time IN $emp_in and then subtract the time out from time in to get your desired output.

$emp_in = '7:00 am';
// find the difference between time entered and 8:00 am
$diff = '8:00 am' -$emp_in;

// multiply the diffrence time to 3600 to change to second and then add with time in
$emp_in = strtotime($emp_in) + ($diff*3600);

$emp_out = '5:00 pm';

$emp_out = strtotime($emp_out);

$result_in_out = $emp_in - $emp_out;
$result_hours = abs(floor($result_in_out/3600));
echo $result_hours;

Hope it works for you <3

    $time1 = '07:00:00';
    $time2 = '05:00:00';
    $array1 = explode(':', $time1);
    $array2 = explode(':', $time2);

    $minutes1 = ($array1[0] * 60.0 + $array1[1]);
    $minutes2 = ($array2[0] * 60.0 + $array2[1]);

    echo $diff = $minutes1 - $minutes2.' Minutes';
<?php

$datetime1 = new DateTime('11:00 AM');
$datetime2 = new DateTime('01:00 PM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%hh %im');

PHP help doc : Click Here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM