0

This is date, The idea of this, it will increment the date by 15th day of the month and end day of the month base on the charge_installment value.

this question is different from this section > How to get every 15th and last day of the month in PHP

$date = '21-JAN-19';
$chrg_installment = 4;

for ($i=1; $i <= $chrg_installment ; $i++) { 
    echo $date;
}

output of this loop.

21-JAN-19 21-JAN-19 21-JAN-19 21-JAN-19


the correct output should be:

  1. 31-JAN-19
  2. 15-FEB-19
  3. 28-FEB-19
  4. 15-MAR-19

3 Answers3

1

To find the last day of the month you can use t as the format parameter supplied to the date() function. To find the 15th of the month generate the time using mktime and convert to a date with required output format.

/* Initial date and duration of processing */
$start = '2019-01-21';
$months = 4;

/* reduce start date to it's constituent parts */
$year = date('Y',strtotime($start));
$month = date('m',strtotime($start));
$day = date('d',strtotime($start));

/* store results */
$output=array();

for( $i=0; $i < $months; $i++ ){
    /* Get the 15th of the month */
    $output[]=date('Y-m-d', mktime( 0, 0, 0, $month + $i, 15, $year ) );
    /* Get the last day of the calendar month */
    $output[]=date('Y-m-t', mktime( 0, 0, 0, $month + $i, 1, $year ) );
}

/* use the results somehow... */
printf('<pre>%s</pre>',print_r($output,true));

Outputs:

Array
(
    [0] => 2019-01-15
    [1] => 2019-01-31
    [2] => 2019-02-15
    [3] => 2019-02-28
    [4] => 2019-03-15
    [5] => 2019-03-31
    [6] => 2019-04-15
    [7] => 2019-04-30
)

If, as the comment below suggests, you would prefer that the dates begin with the month end rather than the 15th of the month simply change the order within the loop that calculated dates are added to the output array...

for( $i=0; $i < $months; $i++ ){
    /* Get the last day of the calendar month */
    $output[]=date('Y-m-t', mktime( 0, 0, 0, $month + $i, 1, $year ) );

    /* Get the 15th of the month */
    $output[]=date('Y-m-d', mktime( 0, 0, 0, $month + $i, 15, $year ) );
}

output:

Array
(
    [0] => 2019-01-31
    [1] => 2019-01-15
    [2] => 2019-02-28
    [3] => 2019-02-15
    [4] => 2019-03-31
    [5] => 2019-03-15
    [6] => 2019-04-30
    [7] => 2019-04-15
)

To output the results in the exact format of the example results

$format=(object)array(
    'last'  =>  't-M-y',
    '15th'  =>  'd-M-y'
);

for( $i=0; $i < $months; $i++ ){
    /* Get the last day of the calendar month */
    $output[]=date( $format->{'last'}, mktime( 0, 0, 0, $month + $i, 1, $year ) );
    /* Get the 15th of the month */
    $output[]=date( $format->{'15th'}, mktime( 0, 0, 0, $month + $i, 15, $year ) );
}

output:

Array
(
    [0] => 31-Jan-19
    [1] => 15-Jan-19
    [2] => 28-Feb-19
    [3] => 15-Feb-19
    [4] => 31-Mar-19
    [5] => 15-Mar-19
    [6] => 30-Apr-19
    [7] => 15-Apr-19
)
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0
   $chrg_installment = 3;
$result=array();
$new_Date="";
for ($i=1; $i <= $chrg_installment ; $i++) { 
    if($i==1){
        $date='21-JAN-19';
        $date=date("t-M-Y", strtotime($date));
        array_push($result, $date);
        $new_Date= date("d-M-Y", strtotime("+15 day", strtotime($date)));
        array_push($result, $new_Date);
    }
    else{
        $date=date("d-M-Y",strtotime('first day of this month'));
         $date=date("t-M-Y", strtotime($new_Date));
        array_push($result, $date);
        $new_Date= date("d-M-Y", strtotime("+15 day", strtotime($date)));
        array_push($result, $new_Date);
    }   


}
print_r($result);

Result : enter image description here

Madhuri Patel
  • 1,270
  • 12
  • 24
  • This might help but the output is greater than from charge installment value. if you declare 3 on the installment, the loop stop unto it. – Rrj Villareal Jan 22 '19 at 02:07
0
$y=1;
$num_term = 10;
//start date
$month_sched = date("2019-01-01");
while($y <= $num_term) {
    //15th
    $month_line_15 = strtotime($month_sched." +14 day");
    //last day of month
    $month_line_last = strtotime($month_sched." next month - 1 hour");
    echo $day = date("M-d", $month_line_15);
    echo $month_int = date("M-d", $month_line_last);
    $month_sched = date("Y-m-d",strtotime($month_sched." +1month"));
    $y++;
}
Pabari Mohit
  • 92
  • 1
  • 2
  • 9