Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

breaking time segments (shifts) by subsegments (day, night, etc)

I have a problem in a small project in PHP and can't think of an algorithm that considers every case. The problem gives a file with two constants and an array of info.

<?php


// The start and end of nighttime hours
$SETTINGS_nighttime_start = '22:00';
$SETTINGS_nighttime_end   = '07:00';

// Employees and their shifts
$EMPLOYEES = array(

    '0' => array(
        'name'        => 'Bernice Lyons',
        'shift_start' => '15:15',
        'shift_end'   => '23:45'
    ),

    '1' => array(
        'name'        => 'Gregg Santos',
        'shift_start' => '10:00',
        'shift_end'   => '22:00'
    ),

    '2' => array(
        'name'        => 'Bennie Montgomery',
        'shift_start' => '22:30',
        'shift_end'   => '08:00'
    ),

    '3' => array(
        'name'        => 'Nelson Austin',
        'shift_start' => '20:00',
        'shift_end'   => '10:00'
    ),

    '4' => array(
        'name'        => 'Garrett Sims',
        'shift_start' => '09:00',
        'shift_end'   => '17:00'
    ),

    '5' => array(
        'name'        => 'Joanna Pratt',
        'shift_start' => '23:00',
        'shift_end'   => '06:00'
    )

);

?>

So I need to write an algorithm that considers every case and calculates time someone worked day and time worked at night.

What statement I should use to cover the case and get right result :

startShift = 20:00
endShift = 10:00

should result

daytimeWork = 05:00
nighttimeWork = 09:00

I have so far got the data and print them on screen but I have a problem with calculating daytime and nighttime shift times

The pseudocode I wrote for this case I wrote is

if(startShift<22:00 && endShift>07:00){
   nighttimeWork = 09:00;
   daytimeWork = shiftLength - 09:00;
}

but in the statement includes the case

startShift = 10:00
endShift = 20:00

I am so frustrated. Please someone help.

like image 623
Andreas777 Avatar asked Dec 19 '25 01:12

Andreas777


1 Answers

This problem requires you to start with shifts: time intervals denoted by start/end times. It requires you to break those shifts into shiftlets: smaller nonoverlapping time intervals the union of which span the same start/end time.

Consider a software operation that takes a shift item as input, and breaks it into shiftlets.

For example:

startShift = 20:00  endShift = 10:00

First break this shift into shiftlets that don't span midnight:

startShift = 20:00  endShift = 00:00
startShift = 00:00  endShift = 10:00

Then break these shiftlets into shiftlets that don't cross the day/night boundary.

startShift = 20:00  endShift = 00:00
startShift = 00:00  endShift = 07:00
startShift = 07:00  endShift = 10:00

Then classify each shiftlet by day or night, then add up the times, and you're done.

like image 191
O. Jones Avatar answered Dec 20 '25 15:12

O. Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!