【问题标题】:For loop 15th day and last day of the current month phpFor循环当月php的第15天和最后一天
【发布时间】:2019-01-21 09:34:00
【问题描述】:

这是日期,这个想法是,它会根据 charge_installment 值将日期增加每月的第 15 天和每月的结束日期。

这个问题与本节不同> 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;
}

这个循环的输出。

19 年 1 月 21 日 19 年 1 月 21 日 19 年 1 月 21 日 19 年 1 月 21 日


正确的输出应该是:

  1. 19 年 1 月 31 日
  2. 19 年 2 月 15 日
  3. 28-FEB-19
  4. 19 年 3 月 15 日

【问题讨论】:

  • 你用 $i 循环做什么?我看不出你的日期和你的循环之间有任何关系
  • @Mukyuu 这似乎只是他想要回应的日期数量,遵循该模式。
  • StackOverflow 不是免费的编码服务。 SO希望您try to solve your own problem first。请更新您的问题以在minimal reproducible example 中显示您已经尝试过的内容。如需更多信息,请参阅How to Ask,并拨打tour :)
  • 如果是这样的话,那就是help
  • @Mukyuu 只是举例

标签: php loops for-loop if-statement


【解决方案1】:

要查找该月的最后一天,您可以使用t 作为提供给date() 函数的格式参数。要查找每月 15 日,请使用 mktime 生成时间并转换为具有所需输出格式的日期。

/* 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));

输出:

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
)

【讨论】:

  • 这可能会有所帮助,最好在 2019-01-31 开始循环,此示例:2019-01-31、2019-02-15、2019-02-28、2019-03 -15
  • 请解释您的评论是什么意思 - 您想以 15th 开头吗?您上面的示例数据从月底开始,所以...
  • 循环数的基数。 $月 = 4;数组([0] => 2019 年 1 月 31 日 [1] => 19 年 2 月 15 日 [2] => 19 年 2 月 28 日 [3] => 19 年 3 月 15 日)
【解决方案2】:
$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++;
}

【讨论】:

    【解决方案3】:
       $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);
    

    结果

    【讨论】:

    • 这可能会有所帮助,但输出大于收费分期付款值。如果您在分期付款中声明 3,则循环停止。
    猜你喜欢
    • 2016-02-09
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多