Jump to content


This topic is now archived and is closed to further replies.


Database Design for Calendar

Recommended Posts

this troubles me for a couple of month now, I haven't started writing anything and it's not urgent to, it's one of them fun-to-do-sometime projects  ::)

I would like to create a staffing schedule calendar and I am wondering how to
do the database layout for this.
The easiest way would be to have one field in the DB per day and per employee but that'll obviously generate masses of data.

I also thought to have a database field per month per employee and then delimiting each day with some character, and sub- definitions of the day with another character.

Would you have any input into this please.

Thansk & Regards,

Share this post

Link to post
Share on other sites
Off the top of my head, I'd use two tables; Employees and Dates (with emp_id as foreign key)

Employees contains only employee info.

Dates contains three fields, week_id INT and emp_id INT shift_pattern INT.

Then use bitwise to denote which days people will work.

Can then use queries like:
[code]SELECT `employee`.`name`, `dates`.`shift_pattern`
FROM `employees`
JOIN `dates`
ON `employees`.`emp_id` = `dates`.`emp_id`
WHERE `dates`.`emp_id` = 1;[/code]

then use bitwise comparison to see which days someone works:


$row = mysql_fetch_assoc($result);

$days = array(
  1 => 'Sun',
  2 => 'Mon',
  4 => 'Tue',
  8 => 'Wed',
  16 => 'Thur',
  32 => 'Fri',
  64 => 'Sat'

for ($i = 1; $i < 64; $i += $i)
    if ($row['shift_pattern'] ^ $i) echo "{$row['name']} is working on {$days[$i]}<br />\n";


Share this post

Link to post
Share on other sites


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.