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
  • what are you using your $i loop for? I don't see any relation between your date and your loop – Mukyuu Jan 21 '19 at 09:35
  • @Mukyuu That seems to be just the number of dates he wants to echo, following that pattern. – Barmar Jan 21 '19 at 09:36
  • StackOverflow is not a free coding service. SO expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592). Please update your question to show what you have already tried in a [mcve]. For further information, please see [ask], and take the [tour] :) – Barmar Jan 21 '19 at 09:37
  • If that is the case, this would [help](https://stackoverflow.com/questions/660501/simplest-way-to-increment-a-date-in-php) – Mukyuu Jan 21 '19 at 09:37
  • @Mukyuu its just for example – Rrj Villareal Jan 21 '19 at 09:40
  • @Barmar Yes, I know. i just post the logic or idea of the date. – Rrj Villareal Jan 21 '19 at 09:40
  • Possible duplicate of [How to get every 15th and last day of the month in PHP](https://stackoverflow.com/questions/33603709/how-to-get-every-15th-and-last-day-of-the-month-in-php) – Mukyuu Jan 21 '19 at 09:44
  • https://stackoverflow.com/questions/1686724/how-to-find-the-last-day-of-the-month-from-date – Barmar Jan 21 '19 at 09:45
  • guys i've already tried those of your suggestions, but those post will not output same that was given above. – Rrj Villareal Jan 21 '19 at 09:52

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
  • 30,177
  • 5
  • 29
  • 43
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,180
  • 12
  • 23
  • 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
  • 84
  • 1
  • 2
  • 8