|
Posted by steve on 09/16/05 22:24
i don't know if anyone will find this helpful but i'll post it just the
same. after programming similar code before, i made a generic class to use
again should the need arise. this class allows one to define shift
information. once defined, it will return the accounting date for a given
transaction...along with other info about the shift. it's pretty basic but
can be extended fairly easily. overtime occurs when a transaction takes
place outside of a defined period of time and its accounting date is
considered to fall under the last shift of the previously defined time
period.
anyway, hope it can be useful to someone...
here's the test data:
first, here's script to test the output of the class...
============= begin snippet ===========
<pre>
<?
$testDates = array(
'09/05/2005 04:00:00' ,
'09/05/2005 03:00:58' ,
'09/07/2005 07:00:58' ,
'09/07/2005 18:22:15' ,
'09/07/2005 08:30:47' ,
'09/08/2005 18:29:59' ,
'09/08/2005 18:30:00' ,
'09/09/2005 05:59:59' ,
'09/09/2005 06:00:00' ,
'09/09/2005 18:29:59' ,
'09/10/2005 18:30:00'
);
foreach ($testDates as $date)
{
foreach ($siteShifts as $workStation => $shifts)
{
$transaction = $shifts->getAccounting($date);
$shift = $transaction->shift;
echo "\r\n" . $workStation . " ; ";
echo "tdate: " . date('D H:i:s', $transaction->dateOfRecord) . " ; ";
echo "adate: " . date('D', $transaction->date) . " ; ";
echo "shift: " . $shift->name . " " .
"(" .
date('H:i:s', $shift->startTime) .
" - " .
date('H:i:s', $shift->endTime) .
")";
echo ($transaction->isOverTime ? ' ; OVERTIME' : '');
}
echo "\r\n";
}
?>
</pre>
============= end snippet =============
next, here's how to define a shift...
============= begin snippet =============
<?
$latheShifts = new shifts();
$pressShifts = new shifts();
$mountShifts = new shifts();
// ================================
$latheShifts->add(
'1' ,
array( 1, 2, 3, 4) , // m-th
mktime(6, 0, 0) , // 6 am
mktime(18, 29, 59) // 6.29.59 pm
);
$latheShifts->add(
'2' ,
array( 1, 2, 3, 4) , // m-th
mktime(18, 30, 0) , // 6.30 pm
mktime(5, 59, 59) // 5.59.59 am
);
$latheShifts->add(
'3' ,
array( 5, 6, 0) , // fri-sun
mktime(6, 0, 0) , // 6 am
mktime(18, 29, 59) // 6.29.59 pm
);
$latheShifts->add(
'4' ,
array( 5, 6, 0) , // fri-sun
mktime(18, 30, 0) , // 6.30 pm
mktime(5, 59, 59) // 5.59.59 am
);
// ================================
$pressShifts->add(
'1' ,
array( 1, 2, 3, 4) , // m-th
mktime(4, 0, 0) , // 4 am
mktime(16, 29, 59) // 4.29.59 pm
);
$pressShifts->add(
'2' ,
array( 1, 2, 3, 4) , // m-th
mktime(16, 30, 0) , // 4.30 pm
mktime(3, 59, 59) // 3.59.59 am
);
$pressShifts->add(
'3' ,
array( 5, 6, 0) , // fri-sun
mktime(6, 0, 0) , // 6 am
mktime(18, 29, 59) // 6.29.59 pm
);
$pressShifts->add(
'4' ,
array( 5, 6, 0) , // fri-sun
mktime(18, 30, 0) , // 6.30 pm
mktime(5, 59, 59) // 5.59.59 am
);
// ================================
$mountShifts->add(
'1' ,
array( 1, 2, 3, 4) , // m-th
mktime(4, 0, 0) , // 4 am
mktime(16, 29, 59) // 4.29.59 pm
);
$mountShifts->add(
'2' ,
array( 1, 2, 3, 4) , // m-th
mktime(16, 30, 0) , // 4.30 pm
mktime(3, 59, 59) // 3.59.59 am
);
$mountShifts->add(
'3' ,
array( 5, 6, 0) , // fri-sun
mktime(6, 0, 0) , // 6 am
mktime(18, 29, 59) // 6.29.59 pm
);
$mountShifts->add(
'4' ,
array( 5, 6, 0) , // fri-sun
mktime(18, 30, 0) , // 6.30 pm
mktime(5, 59, 59) // 5.59.59 am
);
// ================================
$siteShifts['LATHE'] = $latheShifts;
$siteShifts['PRESS'] = $pressShifts;
$siteShifts['MOUNT'] = $mountShifts;
?>
============= end snippet =============
and now, the shift classes...
============= begin snippet ===========
<?
class shift
{
var $name = '';
var $weekDay = -1;
var $startTime = '';
var $endTime = '';
function shift(){}
}
class shifts
{
var $_shift = 0;
var $_shifts = array();
var $_shiftByDays = array();
function shifts(){}
function add($name, $days = array(), $startTime, $endTime)
{
if (!is_array($days)){ $days = array($days); }
sort($days, SORT_NUMERIC);
foreach ($days as $day)
{
$shift = new shift();
$shift->name = $name;
$shift->weekDay = $day;
$shift->startTime = $startTime;
$shift->endTime = $endTime;
$this->_shifts[] = $shift;
$this->_shiftByDays[$day][$endTime] = $shift;
}
}
function _concatDate($time, $date)
{
return mktime(
date('H', $time) ,
date('i', $time) ,
date('s', $time) ,
date('m', $date) ,
date('d', $date) ,
date('Y', $date)
);
}
function getAccounting($date)
{
if (!is_numeric($date)){ $date = strtotime($date); }
$accounting->date = 0;
$accounting->dateOfRecord = $date;
$accounting->isOverTime = false;
$dateProperties = getdate($date);
foreach ($this->_shifts as $shift)
{
if ($shift->weekDay != $dateProperties['wday']){ continue; }
$start = $this->_concatDate($shift->startTime, $date);
if ($shift->endTime < mktime(12))
{
$end = mktime(
date('H', $shift->endTime) ,
date('i', $shift->endTime) ,
date('s', $shift->endTime) ,
date('m', $date) ,
date('d', $date) + 1 ,
date('Y', $date)
);
}
$end = !isset($end) ? $date : $end;
$end = $this->_concatDate($shift->endTime, $end);
if ($date >= $start && $date <= $end)
{
$accounting->date = strtotime(date('m/d/Y', $date));
$accounting->shift = $shift;
return $accounting;
}
}
$previousShift = ($previousShift = $dateProperties['wday'] - 1) < 0 ?
6 : $previousShift;
while (!isset($this->_shiftByDays[$previousShift]))
{
$previousShift = ($previousShift = $dateProperties['wday'] - 1) < 0 ?
6 : $previousShift;
}
foreach ($this->_shiftByDays[$previousShift] as $endOfShift => $shift)
{
if (!isset($targetShift)){ $targetShift = $shift; continue; }
$currentTime = $targetShift->endTime;
$targetTime = $shift->endTime;
if ($currentTime < $targetTime && $currentTime < mktime(12)){
continue; }
$targetShift = $shift;
}
$shift = $targetShift;
if (!isset($shift)){ return $accounting; }
$dateOffset = abs($shift->weekDay - $dateProperties['wday']);
$accounting->date = mktime(
0 ,
0 ,
0 ,
$dateProperties['mon'] ,
$dateProperties['mday'] - $dateOffset ,
$dateProperties['year']
);
$end = $accounting->date;
if ($shift->endTime < mktime(12))
{
$end = mktime(
date('H', $shift->endTime) ,
date('i', $shift->endTime) ,
date('s', $shift->endTime) ,
date('m', $accounting->date) ,
date('d', $accounting->date) + 1 ,
date('Y', $accounting->date)
);
}
$currentTime = $this->_concatDate($date,
$accounting->date);
$start = $this->_concatDate($shift->startTime,
$accounting->date);
$end = $this->_concatDate($shift->endTime, $end);
$accounting->isOverTime = !($date >= $start && $date <= $end);
$accounting->shift = $shift;
return $accounting;
}
}
?>
Navigation:
[Reply to this message]
|