Working Shift Patterns Hi All!
I'm trying to generate a collection of dates within a date range for shift workers working x days on x days off so I can insert working day records into the db.
Right now I have this:
getShiftPattern('2022-11-25', '4', '2022-12-25');
function getShiftPattern($start, $interval, $end) {
$datePeriod = new CarbonPeriod($start, "$interval days", $end);
$shift = 'on';
foreach($datePeriod as $date) {
if($shift == 'on') {
$workDays = CarbonPeriod::create($date->copy()->addDay(1), $date->copy()->addDays($interval - 1));
echo $date->format("d/m/Y"), PHP_EOL;
foreach ($workDays as $days) {
echo $days->format("d/m/Y"), PHP_EOL;
}
echo PHP_EOL;
$shift = 'off';
} else {
$shift = 'on';
}
}
}
(I know it just outputs currently)
I'm just wondering if I can get input on improvements within Laravel or if i'm using CarbonPeriod correctly.
Thanks
Chris
Perhaps you could use this;
use Carbon\Carbon;
$start = Carbon::parse('2022-11-25');
$end = Carbon::parse('2022-12-25');
$insert = $start->copy();
$daysOn = 4;
$daysOff =3;
$shift = [];
while($end->gt($insert)) {
foreach(range(1,$daysOn) as $on)
{
if($insert->gte($end)) break;
$shift[$insert->format('ymd')] = 'On';
$insert->addDay();
}
foreach(range(1,$daysOff) as $off)
{
if($insert->gte($end)) break;
$shift[$insert->format('ymd')] = 'Off';
$insert->addDay();
}
}
dd($shift);
Please sign in or create an account to participate in this conversation.