Michael Reuben Michael Reuben - 5 months ago 23
PHP Question

Php: How can I get the same date of the following months, quarterly and semi-annually

I'm this situation where users can choose to be billed either every month, quarter or semi-annual on one specific date the choose in a billing cycle. I'm using Laravel PHP framework.

Eg. Monthly - 5 May 2016, 5 June 2016, 5 July 2016, etc.
Billed on 5 on every month
Eg. Quarterly - 10 Jan 2016, 10 March 2016, 10 June 2016 etc...
Billed on 10 after every three months
Eg. Semi-Annually - 13 Apr 2016, 13 Sept 016
Billed on 13 after every six months.

The goal here is to get the same date on the every month the user choose to be billed, but in a month, quarterly or semi-annually.

This is my code after I play with @Rishi methode for some time

public function as_you_go($pay_as_you_go,$grand_total,$payment_date,$policy_id)
{
$policy = $this->findorFail($policy_id);
if($pay_as_you_go == 'Semi-Annually'){
$payments_no = 2;
$installment_amount = $grand_total/$payments_no;
$month_gap = 6;
$add_month = '+6 month';
}elseif($pay_as_you_go == 'Quarterly'){
$payments_no = 4;
$installment_amount = $grand_total/$payments_no;
$month_gap = 3;
$add_month = '+3 month';
}elseif($pay_as_you_go == 'Monthly'){
$payments_no = 12;
$installment_amount = $grand_total/$payments_no;
$month_gap = 1;
$add_month = '+1 month';
}

// Pay as you go calculations
for ($x = 0; $x < $payments_no; $x++) {
//$add_month = '+'.$x.' month';
$installment = new \App\Installment;
$installment->amount = $installment_amount;


if(\App\Installment::wherePayableType('App\Policy')->wherePayableId($policy->id)->first()){

$payment = \App\Installment::wherePayableType('App\Policy')->wherePayableId($policy->id)->orderBy('id', 'desc')->first();
$date = $payment->payment_date->format('Y-m-d');
$installment->payment_date = date('Y-m-d',strtotime($date.$add_month));
}else{
$installment->payment_date = $payment_date;
}
$installment->is_payed = 'No';
$policy->installments()->save($installment);
}

}


This is my function I wanted to generates dates according to user's date of choice based on semi-annual, quarterly and monthly payment gap.

Answer

Try like this

<?php
  $date = '1-may-2016';
  echo date('d-F-Y',strtotime($date.'+1 month'))."\n";
  echo date('d-F-Y',strtotime($date.'+3 month'))."\n";
  echo date('d-F-Y',strtotime($date.'+6 month'))."\n";
?>

check output here : https://eval.in/582424

Also you need to think about dates like $date = '30-jan-2016';.

Comments