构建未来日期数组

时间:2017-06-19 20:47:29

标签: php

我正在构建一个交互式网络表单,在用户登录后,他们可以选择向他们的帐户付款...每月的第(n)天或每隔'n'天数 - 他们会选择他们想做多少付款

他们将使用datepicker来选择他们的开始日期(永远不会比明天更早) 我想将这些参数传递给php脚本,然后呈现一个页面,表明他们已同意在以下日期进行x次付款....

我在传递给这个脚本时遇到了数学问题。 也就是说,如果他们选择从2017年6月25日开始每14天进行6次付款......例如......我如何将其放入数组并让结果页面说:您已同意支付x金额:< / p>

06/25/2017
07/09/2017
07/23/2017
08/06/2017
08/20/2017
09/03/2017 
etc

他们可以选择至少1个付款日期或最多12个付款日期。在建造阵列时,有人能引导我朝着正确的方向前进吗?

2 个答案:

答案 0 :(得分:0)

你应该在你的官方日期添加x天,一种方式是:

$next = date("Y-m-d", strtotime('+ 14 days', strtotime($initialdate)));

答案 1 :(得分:0)

试一试......

<?php
  // the date the user selected
  $selectedPaymentDate = strtotime('2017-06-24');

  // the frequency a user will pay
  $freqNum = 14;
  $freqType = 'days'; // days, months, years, whatever `strtotime` will handle

  // the total number of payments a user must make
  $totalPayments = 6;

  // where we'll keep the dates
  $paymentDates = [];

  // build the dates array
  for($i = 0; $i <= $totalPayments; $i++){
      $next = ($freqNum * $i);
      $paymentDates[]= date('Y-m-d', strtotime("+{$next} {$freqType}", $selectedPaymentDate));
  }
相关问题