Jump to content

[SOLVED] Need to calculate a Firefighters Schedule in PHP


snowman2344

Recommended Posts

I need to calculate the following schedule in php. What I need out of the script is what shift is working “today” E.G. $shift = d;.  There are 4 shifts working 24hr days starting at 7 am. The days rotate on 28 day cycles. E.G.  ‘C shift’  is working on July 1 (Sunday) the next Sunday they work is the 29th. I found some code that works with 3 shifts on a different cycle but I have 4 shifts.

 

  $referencePoint = mktime(7, 0, 0, 9, 11, 2004);   // Sept 11, 2004 at 7AM started an A Shift.
  $now = time();
  
  // Next we need to know how many seconds since the start of the last A Shift.
  // First we compute how many 3 days cycles since the reference point then
  // subtract that number.
  
  $difference = ($now - $referencePoint);
  $cycles = floor($difference / (60 * 60 * 24 * 3));
  $sinceReference = ($difference - ($cycles * 60 * 60 * 24 * 3));

  if ($sinceReference < 60 * 60 * 25) {  // Before the start of the 25th hour it's A Shift.
    $shift = "A";
    }
  
  elseif ($sinceReference < 60 * 60 * 49) {  // Else before the start of the 49th hour it's B Shift.
    $shift = "B";
    }
  
  else {
    $shift = "C";  // Else it's C Shift.
    }

 

cal07.JPG

Link to comment
Share on other sites

try

<?php
$start_date = '7/1/2007';
$shift_cycle = array('C',
				 'A','B','C','A','D','C','D',
				 'B','C','D','B','A','D','A',
				 'C','D','A','C','B','A','B',
				 'D','A','B','D','C','B');
$today = date('m/d/y');
//$today = '12/29/2007'; //for testing
echo 'Shift is ', $s = $shift_cycle[((strtotime($today) - strtotime($start_date))/(86400))%28];
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.