Reply
Highlighted
Contributor
Posts: 15
Registered: ‎03-15-2011

Re: How can I get next billing date in ARB subscription?

Thank you so much. That make sense.

Highlighted
Contributor
Posts: 15
Registered: ‎03-15-2011

Re: How can I get next billing date in ARB subscription?

I realize that this is not a PHP forum, but I am looking for suggestions. I can calculate the last day of next month based on the current date so if today is 01/31 I can get 02/28, but I am not sure how to approach this if I am only storing the ARB start date. e.g. Start date = 01/31 and now it is 04/02 and I want to display the next bill date.

 

Any thoughts?

Highlighted
Posts: 1,609
Topics: 15
Kudos: 201
Solutions: 121
Registered: ‎06-23-2011

Re: How can I get next billing date in ARB subscription?

Depends. If you know it's 2011-04-02, then all you have to do is calculate how many days there are in the current month and then put that number in place of the 2, and to do that just requires knowing how many days each month has and how to calculate whether February has 28 or 29 days. Fairly simple. If you don't know it's 2011-04-02, you can use the date function to give you that information.

 

If your subscription started on the 27th, of course, you don't need to even bother with all of that - it's always going to be the 27th.

Highlighted
Contributor
Posts: 15
Registered: ‎03-15-2011

Re: How can I get next billing date in ARB subscription?

[ Edited ]

I think I have it for anyone else who is interested. Any thoughts and/or improvements are appreciated.

 

/**
 * Function to calculate the same day one month in the future.
 *
 * This is necessary because some months don't have 29, 30, or 31 days.
 * If thenext month doesn't have as many days as this month, the 
 * anniversary will be moved up to the last day of the next month.
 *
 * @param $start_date
 *    Function assumes that date is pulled from MySql YYYY-MM-DD
 *
 * @return $nextBillDate
 *    Formmated in common US date format MM/DD/YYYY
 */

function getNextBillDate($start_date) {

   $date_array = explode("-",$start_date); // split the array

   $year = $date_array[0];
   $month = $date_array[1];
   $day = $date_array[2];

   if (date("d")  $day) {
      $billDay = $day;
   }else{
      $billDay = $billMonthDays;
   }

   $nextBillDate = $billMonth . "/" . $billDay . "/" . date("Y");

   return $nextBillDate;
}
 
Highlighted
Posts: 1,609
Topics: 15
Kudos: 201
Solutions: 121
Registered: ‎06-23-2011

Re: How can I get next billing date in ARB subscription?

Well, you're getting a bit closer, but there seem to be some errors and no explanation of where $billMonthDays is coming from.

Highlighted
Contributor
Posts: 15
Registered: ‎03-15-2011

Re: How can I get next billing date in ARB subscription?

Sorry about that, several lines keeping getting removed when I paste and I did not notice it before...

 

function getNextBillDate($start_date) {

   $date_array = explode("-",$start_date); // split the array

   $year = $date_array[0];
   $month = $date_array[1];
   $day = $date_array[2];

   if (date("d") <= $day) {
      $billMonth = (int)date("m");
   }else{
      $billMonth = date("m")+1;
   }
   $billMonthDays = cal_days_in_month(CAL_GREGORIAN, ($billMonth), date("Y"));

   if ($billMonthDays > $day) {
      $billDay = $day;
   }else{
      $billDay = $billMonthDays;
   }

   $nextBillDate = $billMonth . "/" . $billDay . "/" . date("Y");

    return $nextBillDate;
}

Highlighted
Contributor
Posts: 15
Registered: ‎03-15-2011

Re: How can I get next billing date in ARB subscription?

Someone brought an error to my attention. The following returns an error if we are in the month of December...

 

$billMonth = date("m")+1;

 

If you are using PHP 5.3+, you can use the following...

 

$billMonth = date_format(date_modify(date_create(), 'first day of next month'), 'm');