简体   繁体   English

如何在PHP4中生成付款截止日期的列表?

[英]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. 我看到所有好的日期/时间函数都仅是PHP5,这使这一点变得很困难。 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. 我需要生成一个所有薪资截止日期的列表,该日期从52周开始,到将来的一个薪资期间,每两周一次。

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. 因此,假设下一个支付期是2010年6月26日,今天是2010年6月23日,我想要一个清单(以(大约) 2009年 6月26日开始,以2010年6月26日结束)。

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. 如果日期与付款期的结束日期是同一天,则我要包括下一个付款期:如果下一个付款期的结束日期是2010年6月26日,今天是2010年6月26日 ,我希望列出以(大约) 2009年 6月26 至2010年7月10日结束。

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? 仅使用PHP 4.3中可用的工具来执行此操作的最佳方法是什么?

You can get an awful lot done with strtotime() . 您可以使用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. 可能不完全准确,但是您应该了解一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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