Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phayes0289's avatar

How To Build A Reusable Function That Identifies A Shift Workers Schedule

I am looking for something that will give me shift information for people that work shifts (Nurses, Police, Fire, EMS and 24-hour industry). In Coldfusion I had a simple script that I could use to get the shift working given three items.

  1. The current data
  2. The pattern of the shift
  3. The start date of the pattern

Here is the script.

<cfscript>
/**
 * @param Date      Date to return the shift for. (Required)
 * @param StartDate      Start date of the sequence. (Required)
 * @param Sequence      Comma delimited list defining the shift sequence. (Required)
 * @return Returns a string. 
 */
function GetShift(date, startDate, sequence) {
  var daysDiff = DateDiff("d",startDate,date);
  var key = daysDiff mod listLen(sequence) + 1;
  return listGetAt(sequence,key);
}
</cfscript>

Here are some of the patterns that I have used: OFF,24,OFF,OFF D,D,D,D,D,OFF,OFF,D,D,D,D,R/O,OFF,OFF E,D,D,D,RO,OFF,OFF,E,D,D,D,D,OFF,OFF,E,D,D,D,RO,OFF,OFF,E,D,D,D,D,OFF,OFF

This is how the User Defined Function would call the script in ColdFusion.

<cfoutput>#GetShift('#dateformat(url.testdate, "mm/dd/yyyy")#', "1/1/2018", "E,D,D,D,D,OFF,OFF,D,D,D,E,RO,OFF,OFF,D,D,E,D,D,OFF,OFF,D,E,D,D,RO,OFF,OFF")#</cfoutput>

Typically I would input the start date and pattern from a session variable into a user defined function to determine if a certain person was working on a certain date. How should I implement something like this in Laravel?

0 likes
2 replies
LaryAI's avatar
Level 58

In Laravel, you can create a reusable function that identifies a shift worker's schedule by following these steps:

  1. Create a new file called ShiftHelper.php in the app/Helpers directory (if the directory doesn't exist, create it).

  2. Open the ShiftHelper.php file and define a new function called getShift:

<?php

namespace App\Helpers;

class ShiftHelper
{
    public static function getShift($date, $startDate, $sequence)
    {
        $daysDiff = floor((strtotime($date) - strtotime($startDate)) / (60 * 60 * 24));
        $sequenceArray = explode(',', $sequence);
        $key = ($daysDiff % count($sequenceArray)) + 1;
        return $sequenceArray[$key - 1];
    }
}
  1. Save the file and open the config/app.php file.

  2. Add the following line to the aliases array:

'ShiftHelper' => App\Helpers\ShiftHelper::class,
  1. Now you can use the getShift function anywhere in your Laravel application. For example, in a controller or a blade template, you can call the function like this:
use ShiftHelper;

...

$shift = ShiftHelper::getShift($date, '1/1/2018', 'E,D,D,D,D,OFF,OFF,D,D,D,E,RO,OFF,OFF,D,D,E,D,D,OFF,OFF,D,E,D,D,RO,OFF,OFF');

Make sure to replace $date with the actual date you want to check.

This solution creates a static function getShift in a helper class called ShiftHelper. The function takes three parameters: $date, $startDate, and $sequence. It calculates the number of days between the $startDate and $date, then uses the modulus operator to determine the index of the shift in the $sequence array. Finally, it returns the shift at that index.

Note: The ShiftHelper class is just an example. You can place the function in any class or namespace that suits your application structure.

phayes0289's avatar

@LaryAI Almost Perfect! What you gave me works great along as the pattern start date is after March 14th 2013 or so. If I got with a pattern start date before that, It incorrectly jumps to the next shift in the sequence. The only thing I can think of that could be affecting the code you have given me is Daylight Savings Time. It took effect on March 12, 2023. Could this somehow be effecting the code that would cause this jump?

Please or to participate in this conversation.