Using PHP 5.4.45
I've been working on a pretty simplistic timesheet to allow users to clock-in and clock-out, see their hours, etc. So far, everything works pretty smooth, but, I can't seem to figure out a way to split work days by work week. Right now, it will display every hour you've worked day by day. My thought was to create a parent Key => Value array to the last Monday (or start of week) to act as the sorting method.
CREATE TABLE `#__handiclock` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(10) NOT NULL,
`location` varchar(255) DEFAULT '0.0.0.0',
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modId` int(10),
`modified` TIMESTAMP,
`state` tinyint(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
With the current code, each clock in and out is stored in a database separately. STATE 1 is clock in, 0 is clock out. This leaves ample room for possible future states of "break" or "paid vacation" etc. By default, the system will use TIMESTAMP, but an admin can alter the hours by setting MODIFIED, so that the original time is never altered.
function parseTimesheet($info) {
$timesheet = new TimeSheet;
$timesheet->User = JFactory::getUser()->name;
$timesheet->Clocks = array();
$in = 0; $out = 0;
$clock = new Clock;
$clock->Time = strtotime('0000-00-00 00:00:00');
$clock->Breaks = 0;
$clock->Hours = 0;
$day = "01/01/1970";
foreach ($info as $i => $row) {
if ($row->state == 1) {
// Clock IN
$day = date("m/d/Y", strtotime(($row->modified != '0000-00-00 00:00:00') ? $row->modified : $row->timestamp));
$in = strtotime(($row->modified != '0000-00-00 00:00:00') ? $row->modified : $row->timestamp);
if (array_key_exists($day, $timesheet->Clocks)) {
$clock = $timesheet->Clocks[$day];
$clock->Breaks += ($in - $out);
} else {
$timesheet->Clocks[$day] = new Clock();
$clock = $timesheet->Clocks[$day];
}
$clock->Time = date("g:i A", strtotime(($row->modified != '0000-00-00 00:00:00') ? $row->modified : $row->timestamp));
} else {
// Clock OUT
$newday = date("m/d/Y", strtotime(($row->modified != '0000-00-00 00:00:00') ? $row->modified : $row->timestamp));
if ($day != $newday) {
// crossed midnight, split accordingly
$mndate = new DateTime(($row->modified != '0000-00-00 00:00:00') ? $row->modified : $row->timestamp);
$mndate->setTime(12,0,0);
$midnight = $mndate->getTimestamp();
$clock->Hours += ($midnight - $in);
$timesheet->Clocks[$day] = $clock;
$_clock = new Clock; // tomorrows time
$in = $midnight;
$out = strtotime(($row->modified != '0000-00-00 00:00:00') ? $row->modified : $row->timestamp);
$_clock->Hours = ($out - $in);
$timesheet->Clocks[$newday] = $_clock;
$clock = $timesheet->Clocks[$newday];
} else {
$out = strtotime(($row->modified != '0000-00-00 00:00:00') ? $row->modified : $row->timestamp);
$clock->Hours += ($out - $in);
$timesheet->Clocks[$day] = $clock;
}
}
}
//echo '<br>Parsing Complete!';
return $timesheet;
}
The code basically splits each day, and accounts for midnight cross-overs. It automatically calculates break hours, but faults with midnight cross-overs. Not important, as this is more of an estimation factor for an admin to look deeper into.
The actual problem is trying to figure out a math function that will create a KEY same for all the days of the week, but unique for week to week.