简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM