开发者

How can I generate a list of pay-period-ending dates in PHP4?

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜