【问题标题】:Not a duplicate : How to display date of next 6 months in php不重复:如何在 php 中显示未来 6 个月的日期
【发布时间】:2017-10-24 14:49:11
【问题描述】:

我正在研究贷款滑块计算,如果用户从下拉列表中选择任何日期,那么我必须显示 ext 6 个月的日期。 例如:如果用户选择日期“5” 那么接下来的 6 个日期将是我现在得到的日期:

1 5th Dec 2017
2 5th Jan 2018
3 5th Feb 2018
4 5th Mar 2018
5 5th Apr 2018
6 5th May 2018

但如果用户从下拉列表中选择"30" or "31",则它会跳过二月份。

请帮忙!!!!

【问题讨论】:

标签: php date strtotime


【解决方案1】:

考虑这个函数:

    function add($date_str, $months)
    {
        $date = new DateTime($date_str);


        $start_day = $date->format('j');


        for($i=1;i<=$months;$i++)
{ 
        $date->modify("+{$i} month");


        $end_day = $date->format('j');

        if ($start_day != $end_day)
        {

            $date->modify('last day of last month');
        }

        echo  $date;
    }
    }

调用add(date,6);

【讨论】:

    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    相关资源
    最近更新 更多