This is a continuation of my previous question.
UPDATE: Thank you Rasclatt for the cleaner script. It is overall better than my original so I'll be using that instead.
This is what's inside the db, showing the records for ACOL. The query used is the same one I used in the script. ACOL's DATEIN ends at 2015/01/30 and started back on 2015/03/04

This is the output. The row with the orange times indicates TIMEIN while red represents TIMEOUT (not shown on the db). Notice how ACOL have records for February even though there is none in the db.

As you can see, some time for the correct existing dates matches those in the db (4th, 17th) but some are completely missing.

No other instances of ACOL exist that might have caused the inaccuracies. I've queried to see if the TIMEIN/OUT pairs matched someone else's but there's no result. I have no idea where the time came from.
The script
<?php
function is_weekend($date = false)
{
return (date("D",strtotime($date)) == 'Sun' || date("D",strtotime($date)) == 'Sat')? true : false;
}
// Make a function for on time, makes that part changeable
// gives the ability to change start times ($default) in the future
function on_time($timein = false, $default = '09:30')
{
if($timein == false)
return true;
return (strtotime($timein) > strtotime($default))? false : true;
}
function early($timeout = false, $default = '18:00')
{
if($timeout == false)
return true;
return (strtotime($timeout) < strtotime($default))? false : true;
}
$staff = $dome = array();
while($row = ibase_fetch_assoc($result))
{ $dome[] = $row; }
foreach($dome as &$value)
if (ctype_upper(substr($value['NAME'],0,2))== TRUE)
$staff[] = $value['NAME'];
$staff = array_values(array_unique($staff,SORT_REGULAR));
$m = $_POST['month'];
$y = $_POST['year'];
//reorganize the array by day
foreach($dome as $user) {
if(!preg_match('/^'.$y.'\/'.$m.'/',$user['DATEIN']))
continue;
$new[ltrim(substr($user['DATEIN'],-2),"0")][strtolower($user['NAME'])][] = $user['TIMEIN'];
$newto[ltrim(substr($user['DATEIN'],-2),"0")][strtolower($user['NAME'])][] = $user['TIMEOUT'];
}
if ($m != '' || $y != '')
echo "<br><u>Attendance for ".date("F", mktime(null, null, null, $m, 1)).", ".$y."</u>";
else
break;
?>
<table id="caltable" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
NAME
</td>
<?php
// Set header row
$day_in_mo = cal_days_in_month(CAL_GREGORIAN,$m,$y);
for($i = 1; $i <= $day_in_mo; $i++) { ?>
<td><?php echo $i; ?></td>
<?php
} ?>
</tr>
<?php
// Loop through staff
foreach($staff as $name) { ?>
<tr>
<td rowspan=2><?php echo $name; ?></td>
<?php
$keyname = strtolower($name);
for($i = 1; $i <= $day_in_mo; $i++) {
$timein = (!empty($new[$i][$keyname][0]))? $new[$i][$keyname][0] : false;?>
<td class="<?php if(!is_weekend("{$y}-{$m}-{$i}")) { echo 'weekday'; if(!on_time($timein)) echo ' late'; echo '"'; } else echo 'weekend' ?>"><?php
// Match keys and see if user is listed in that day
echo ($timein != false)? $timein : '<div class="absent">-</div>'; ?></td>
<?php
} ?>
</tr>
<tr>
<td style="display:none"></td>
<?php
$keyname = strtolower($name);
for($i = 1; $i <= $day_in_mo; $i++) {
$timeout = (!empty($newto[$i][$keyname][0]))? $newto[$i][$keyname][0] : false; ?>
<td class="<?php if(!is_weekend("{$y}-{$m}-{$i}")) { echo 'weekday'; if(!early($timeout)) echo ' early'; echo '"'; } else echo 'weekend' ?>"><?php
echo ($timeout != false)? $timeout : '<div class="absent">-</div>'; ?></td>
<?php
} ?>
</tr>
<?php
} ?>
</table>