1

Hello Im just a beginner programmer but Im now building an app which gets the total hours of work.

Here I have a time_start array which the threshold for the customer to in and same as out for time_end

The logs here is where the person checks in.

            $time_start = [
                [0] => '22:00:00',
                [1] => '03:00:00',
                ];
            $time_end = [
                [0] => '02:00:00',
                [1] => '08:00:00',
                ];
            $logs = [
                [0] => '2019-07-09 22:00:00',
                [1] => '2019-07-10 02:00:00',
                [2] => '2019-07-10 03:00:00',
                [3] => '2019-07-10 08:00:00',
            ];

So I'm trying to get the sum of their hours in night shift which has different date.

Im trying to get it in:

            foreach( $logs as $check_time ){

                $attendance_date_time = date("Y-m-d",strtotime($check_time));
                $time_starts = date('Y-m-d H:i:s', strtotime("$attendance_date_time $time_start"));
                $time_ends = date('Y-m-d H:i:s', strtotime("$attendance_date_time $time_end"));
                if ( $check_time >= $time_starts && $check_time <= $time_ends )
                {
                    $time[] = $check_time;
                }else{
                    $time = null
                }

            }

Supposedly I will get all logs because it is between time_start and time_end and store it to $time

And summing the time i get.

Since I'm new at php is their any easiest idea to get the total work hours? Cuz I get a null because the time_end it gets was the date of check_in.

5
  • summing means what exactly total hours between those dates? Commented Jul 10, 2019 at 6:00
  • Yes. In the given logs the result will be 9hours Commented Jul 10, 2019 at 6:01
  • How it will be 9 hours, can you explain? Because I see its 5 hours! Commented Jul 10, 2019 at 6:02
  • 2019-09-09 22:00 to 2019-09-10 02:00 equals to 4hours. 2019-07-10 03:00:00 to 2019-07-10 08:00:00 equals to 5hours Commented Jul 10, 2019 at 6:07
  • Since the $logs are beetween the time_in and out it will add to $time array. And add each array. equals to 9hours Commented Jul 10, 2019 at 6:08

2 Answers 2

1

Here is your snippet,

$time_start = [
    0 => '22:00:00',
    1 => '03:00:00',
];
$time_end = [
    0 => '02:00:00',
    1 => '08:00:00',
];
$logs = [
    0 => '2019-07-09 22:00:00',
    1 => '2019-07-10 02:00:00',
    2 => '2019-07-10 03:00:00',
    3 => '2019-07-10 08:00:00',
];
$time = [];
$diff = 0;
for ($j = 0; $j < count($logs); $j = $j + 2) {
    $attendance_start = date("Y-m-d", strtotime($logs[$j]));
    $attendance_end   = date("Y-m-d", strtotime($logs[$j + 1]));
    for ($i = 0; $i < count($time_start); $i++) {
        $time_starts = date('Y-m-d H:i:s', strtotime("$attendance_start " . $time_start[$i] . ""));
        $time_ends   = date('Y-m-d H:i:s', strtotime("$attendance_end " . $time_end[$i] . ""));
        if ($logs[$j] >= $time_starts && $logs[$j + 1] <= $time_ends) {
            array_push($time, $logs[$j], $logs[$j + 1]);
            // calculating diff for two alternate days
            $diff += (round((strtotime($logs[$j + 1]) - strtotime($logs[$j])) / 3600, 1));
            break;
        }
    }
}
echo $diff . " Hours";die;

Demo

Output:-

9 Hours
Sign up to request clarification or add additional context in comments.

3 Comments

2019-09-09 22:00 to 2019-09-10 02:00 equals to 4hours. 2019-07-10 03:00:00 to 2019-07-10 08:00:00 equals to 5hours We have a date 2019-09-09 and 2019-09-10
@ElliotA I have made changes as per your requirement. PLease have a look
thank you. link in case you can help me with this one?
0

The for loop is the easiest way to do this

$time_start = [
  '22:00:00',
  '03:00:00',
];
$time_end = [
  '02:00:00',
  '08:00:00',
];
$logs = [
  '2019-07-09 22:00:00',
  '2019-07-10 02:00:00',
  '2019-07-10 03:00:00',
  '2019-07-10 08:00:00',
];

$totalDiff = 0;

for($i =0 ; $i < count($logs); $i = $i + 2)
{
  $startDate = strtotime($logs[$i]);
  $endDate = strtotime($logs[$i+1]);

  $diff = $endDate-$startDate;

  $totalDiff += $diff;
}

$hourDiff = $totalDiff / 3600;
echo $hourDiff . ' hours';

Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.