Jump to content

[SOLVED] Calculation formula problem dealing with days


NerdConcepts

Recommended Posts

This seems to be a massive problem that I am having and have tried tons of things to figure it out and cannot seem to write a formula to deal with it. This is calculating payroll dates

 

Here is what is stored in the database

example values::

Pay Period: 7 days

Pay Roll Ends: Thursday

 

I don't need a begin day, since it takes the end date and counts back however days the pay period is, takes those dates and grabs payroll. In this example the whole pay period ends up being Friday-Thursday

 

I cannot figure out to calculate the number of days from Wednesday to the previous Thursday. Yes, I know it's 6 days, but what if these days change? The number of days that have to be counted back to get the end of the pay week changes. Example: if christmas falls on wednesday and we do payroll on monday. We will want the previous Thursday to come up (like normal), not the full 6 days back.

 

Here is how I get the day of the week that it currently is.

 

$now = strtotime("Now");

$dotw = date("N", $now);

 

that returns the day number. ie: Sunday=0, Saturday=6

 

but how in the heck do you calculate from days day of the week to last Thursday, find out how many days back that is...I have to figure it out since I will be making actual dates to query the database to grab invoices and what not. Am I just going about this all wrong or something? if the database needs some changes, that is fine. I am the only one working on this. I do have a lot of test pieces done already but that is with me forcing 6 days and faking that today is Wednesday.

 

note: I have read this over multiple times and hope it's not to confusing....

Link to comment
Share on other sites

What I am looking for is a way to calculate how many does back is the previous Thursday. Or whatever day I set in the database. I do believe I have figured it out. Not sure why I didn't think about it before.

 

$now = strtotime("Now");
$dotw = date("N", $now);
$dotw = $dotw + 1; // doing this to get a positive number
$payend = $row['pay_on'] + 1; // Thursday: 4, adding 1 to ensure positive number for math
$paytime = $row['pay_duration']; // 7 days

if ($dotw > $payend) {
$countback = $dotw - $payend;
} elseif $dotw < $payend) {
$countback = $dotw + (7 - $payend);
} else {
$countback = 7;
}

$end_date = strtotime("-" . $countback . " days");

 

That in theory gives me the the end date of the previous payroll week. Haven't tested it, but in theory it will (no matter which days are set in the database) give me the number of days back that the previous work week ends on, then I calculate the actual date of the date, and use the total number of days in a pay period (in this case 7) and I'll have the two dates needs for a database query. Now Time to test it, :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.