tags:

views:

30

answers:

1

I see that all of the good date/time functions are PHP5 only, which sort of makes this hard. I need to generate a list of all pay-period-ending dates starting with 52 weeks back and going to one pay period in the future, with bi-weekly periods.

So, suppose the next pay period ending is June 26, 2010 and today is June 23, 2010, I'd want a list starting with (approximately) June 26, 2009 and ending with June 26, 2010.

If the date is the same day as the pay period ending, I want to include the next one: If the next pay period ending is June 26, 2010 and today is June 26, 2010, I'd want a list starting with (approximately) June 26, 2009 and ending with July 10, 2010.

So, here'd be the call:

>>> get_pay_period_ending_dates($timeInSecondsFromEpoch);
[
  $date52weeksBeforeTheNextPayPeriodEnding,
  $date50weeksBeforeTheNextPayPeriodEnding,
  ...
  $nextPayPeriodEnding
]

Obviously, that's an approximation of the dates I want. Preferably, in seconds since epoch.

What's the best way to do this, using only tools available in PHP 4.3?

+2  A: 

You can get an awful lot done with strtotime(). It feels a little dirty, but it keeps things simple.

$startDate = strtotime('June 26, 2010');

for($i = 0; $i <= 52; $i+=2) {
  echo date('m-d-y', strtotime("-$i weeks", $startDate)), "\n";
}

I believe that's a decent starting point/proof of concept.

Update with calculation of start date:

function startDate($date) {
  $knownDate = strtotime('June 26, 2010');
  $diff = $date - $knownDate;
  $weeks = 2 * ceil($diff / (60*60*24*7*2));
  return strtotime("$weeks weeks", $knownDate);
}

$startDate = startDate(strtotime('June 26, 2011'));

for($i = 0; $i <= 52; $i+=2) {
  echo date('m-d-y', strtotime("-$i weeks", $startDate)), "\n";
}

Might not be completely accurate, but you should get the idea.

Frank Farmer
That doesn't tell me how to find the _first_ pay period date after the current date; is there an easy way to do that?
Chris R
What are the business rules for determining the first pay period date?
Frank Farmer
Easier to describe the last pay period date, but all in terms of a specific date.1. It is known that 2010-06-26 is a pay period end date.2. The next pay period date from any given date is the date after the given date that is a multiple of 2-week periods away from the known date.It may be that there's a more elegant way to frame that description, but I'm not sure what it'd be.
Chris R