开发者

Get a $date between start and end dates on a continuous loop

Trying to figure out a way of achieving the following scenario:-

The user creates an initial time per开发者_如何学Goiod e.g. 01/01/2013 to 01/07/2013 and a renewal period e.g. every 1 month.

I'm working on a function that will:-

  1. Detect whether the date passed in a function (could be any date) falls a period matching the users requirements.

For example:-

The function may accept the following date 21/02/2019. Based on this I need to detect which renewal period the user is in.

The way I was thinking of achieving this is by:-

  1. Add one day to the users initial start date to get the newest renewal date.
  2. Add the renewal period (1 month) to this to get the newest end date.
  3. Keep doing this until I detect which start and end dates the date falls between based on the users renewal period type e.g. 1 month.

A bit confusing but this kind of sums up what I'm after but isn't working:-

$tmpStartDate=$endDate;   
do{
$tmpStartDate=date("Ymd",strtotime($tmpStartDate .'+1 Day'));
$tmpEndDate=date("Ymd",strtotime($tmpStartDate .'+'.$timingUnitVal .' '.$timingUnit));
} while($date<$tmpStartDate&&$date>$tmpEndDate);

$endDate is the end date initially entered by the user.


From what I can gather from your question, something roughly along these lines would be more correct?

function findPeriod($lowerBound, $upperBound, $repeatEvery, $date)
{
  $lowerBound = strtotime($lowerBound);
  $upperBound = strtotime($upperBound);
  $repeatEvery = strtotime('+' . $repeatEvery) - time();
  $date = strtotime($date);

  while ($date >= $lowerBound) {
    if ($date <= $upperBound) {
      return array($lowerBound, $upperBound);
    } else {
      $lowerBound += $repeatEvery;
      $upperBound += $repeatEvery;
    }
  }

  return false;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜