За цикл 15 числа и последний день текущего месяца php - PullRequest
0 голосов
/ 21 января 2019

Это дата. Идея этого заключается в том, что она будет увеличивать дату на 15-й день месяца и заканчивать день месяца на основе значения charge_installment.

этот вопрос отличается от этого раздела> Как получить каждый 15-й и последний день месяца в PHP

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

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

вывод этого цикла.

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


правильный вывод должен быть:

  1. 31-ЯНВ-19
  2. 15-февраль-19
  3. 28-февраль-19

Ответы [ 3 ]

0 голосов
/ 21 января 2019

Чтобы найти последний день месяца, вы можете использовать t в качестве параметра формата, предоставленного функции date(). Чтобы найти 15-е число месяца, с помощью mktime сгенерируйте время и конвертируйте в дату с требуемым выходным форматом.

<code>/* 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
», print_r ($ выход, правда));

Выходы:

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
)

Если, как следует из приведенного ниже комментария, вы бы предпочли, чтобы даты начинались с конца месяца, а не 15-го числа месяца, просто измените порядок в цикле добавления вычисляемых дат в выходной массив ...

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 ) );
}

выход:

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
)

Для вывода результатов в точном формате результатов примера

$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 ) );
}

выход: * * тысяча двадцать-один

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
)
0 голосов
/ 21 января 2019
$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++;
}
0 голосов
/ 21 января 2019
   $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);

Результат : enter image description here

...