Best way to determine and store Pay Periods for a time clock in PHP and MySQL

2.6k views Asked by At

I am building a Time Clock application with PHP and Laravel 4.

My boss requires that he is able to pull and build different reports based on the data I store in the database for a Time Card record.

Right now I store a DateTime for clock in and clock out as well as a Timestamp for both those times as well into the Database.

I need to be able to Query the database and build reports for different Pay Periods for a user.

So for example I will store in another Database Table, records that will be for a User ID and will have different Pay Periods. So a Start day may be the 1st of the month and end date the 15th and that is 1 pay period (roughly 2 weeks) I am not sure the best way to store these records really.

Another will be the 16th of the month to the end of the month. So the end date would be different depending on how many days are in a month

I am not sure about the best way to define these Pay periods for a user. I can't simply say 1-15 and then 16-30 since the 30 would be a different number for each month.

Would appreciate any insight into how this could be done?

So I can build reports for any Pay Periods since not every user gets paid every 2 weeks it needs to be flexible so that I can define it on a per user basis

This question is more about the Logic instead of actual code.

2

There are 2 answers

6
Shawn On BEST ANSWER

I think you are over thinking this. Let thte user define the start and end dates.

You will need the UserId, a timestamp (time in and time out) of the user and that should be about it.

I picture something like this:

UserId | DateIn | DateOut

On the page you could put put dropdowns (or if you want a nifty interface a datepicker that uses javascript) and allow the manager to pick a start and end date that he wants to choose.

So if he wants to see an employees time between Jan. 1 and Feb. 31 he can choose those as his start and end dates.

This will allow things to be very flexible, for example the manager can choose Feb 16 as start date and Feb 29 as end date. It makes sense to allow him to choose the data requirements so he can view whatever he wants.

EDIT:

An example from my comment below this post you could do something like:

$startDate = new DateTime();
$startDate->modify('first day of this month'); //or 16th for second part of bi-monthly
$startDate->format(#some date formatting as you need#);

$endDate = new DateTime();
$endDate->modify('last day of this month'); //or 15th for first part of bi-monthly
$endDate->format(#some date formatting as you need#);

If things are even less defined however you could always try doing special math. date('t') will give you the number of days in a month. I would refrain from using this unless your pay days are fixed such as paid every 6 days.

In general I would harness the power of the PHP DateTime class over using date() function. http://php.net/manual/en/class.datetime.php

0
Matt Johnson-Pint On

Welcome to the wonderful world of Time and Attendance. You are touching the tip of the iceberg. You may find that purchasing a pre-packaged product may be easier than writing your own.

That said, I can offer you the following general advice:

  • Be very careful of your data types and how they are used, both in PHP and in MySQL.

  • You need to make sure you understand local time vs UTC, time zones, and daylight saving time. In general, you don't want to store local time unless you also store its offset from UTC. Otherwise you will have ambiguity around daylight saving time changes. This is important even if you only have one time zone to deal with.

  • When it comes to Pay Periods, the common types are:

    • Weekly
    • Bi-Weekly
    • Semi-Monthly
    • Monthly
    • Every X days starting from Y

  • In some systems, each individual pay period can be adjusted +/- a number of days from it's normal date. When doing so, the bordering period must also be adjusted to compensate.

  • You should start with business logic that can calculate the start and end date for a pay period given any particular date and time. You can then expand that to easily get the prior or next pay period.

  • You can store each pay period into it's own table, but it's not necessarily required. That will depend on a lot of specifics about your system internals.

  • Because a pay period is defined by dates, you have the "Whose Day is it?" problem. It might be the day as defined by the company, or if employees are in different time zones, then it might be the "logical day". If you only have one time zone to deal with then, you are lucky in this regard.

  • When comparing against the pay period, use half-open intervals, [start, end). In other words:

    periodStart <= punchTime < periodEnd
    

    or likewise

    periodStart <= punchTime && periodEnd > punchTime
    

    The end of one period should be exactly the same as the start of the next. Don't try to define the end of the period at some silly value like 23:59:59.999...

As you can see, this is just the beginning. I hope this is useful to you. If you can narrow the focus of your question further, I'll be happy to help more. Otherwise, it's like asking for how to build an ERP system when you're not sure what structure to store inventory.