如何从PHP中的给定日期计算结算周期?

时间:2015-05-01 14:10:37

标签: php date billing

我想得到如下结果。

结算日的基准点:21

正常结算周期为2015-01-21至2015-02-20,2015-02-21至2015-03-20,2015-03-21至2015-04-20,....... ..

用于计算结算周期的第一个月。

当客户在2015-01-21(结算日21)之前注册开始日期(2015-01-13)和结束日期(2015-05-10)时, 我想在2015-01-13到2015-01-20期间做一段时间。

但如果客户在2015-01-21之后注册开始日期(2015-01-25)和结束日期(2015-05-25),我想在2015-01-25至2015-02期间注册-20

上个月

如果客户结束注册是在结算日(2015-05-21)之前的2015-05-10,我想制作2015-04-21至2015-05-10的期间。

客户结束注册是在结算日(2015-05-21)之后的2015-05-25,我想在2015-05-21至2015-05-25期间进行。

ex1)鉴于开始日期:2015-01-10,截止日期:2015-03-19

结果:

$array[0]['start'] = 2015-01-10;
$array[0]['end'] = 2015-01-20;
$array[1]['start'] = 2015-01-21;
$array[1]['end'] = 2015-02-20;
$array[2]['start'] = 2015-02-21;
$array[2]['end'] = 2015-03-19;

ex2)鉴于开始日期:2015-01-24,截止日期:2015-03-25

结算日:21

结果:

$array[0]['start'] = 2015-01-24;
$array[0]['end'] = 2015-02-20;
$array[1]['start'] = 2015-02-21;
$array[1]['end'] = 2015-03-20;
$array[2]['start'] = 2015-03-21;
$array[2]['end'] = 2015-03-25;

ex3)鉴于开始日期:2015-01-24,截止日期:2015-04-18

结算日:21

结果:

$array[0]['start'] = 2015-01-24;
$array[0]['end'] = 2015-02-20;
$array[1]['start'] = 2015-02-21;
$array[1]['end'] = 2015-03-20;
$array[2]['start'] = 2015-03-21;
$array[2]['end'] = 2015-04-18;

我做了如下功能,但这个功能有上个月的问题。

function dateRange($firstDate, $lastDate, $step = '+1 day', $format = 'Y-m-d', $period='21' ){
        $dates = array();

        $first  = strtotime($firstDate); 
        $current= strtotime($firstDate);
        $last   = strtotime($lastDate);

        $startSet = $period;
        $endSet = $period - 1;

        $startFormat = "Y-m-{$startSet}";
        $endFormat = "Y-m-{$endSet}";

        $startMonth = date("Y-m", $first);
        $endMonth = date("Y-m", $last);

        $i=0;

        while( $current <= $last ){    

            if($first==$current){
                // first month

                $dates[$i]['start'] = $firstDate;

                $chkStartDay = date("d", $first);
                if($chkStartDay < $period){

                    // end period of first month.
                    $dates[$i]['end'] = date($endFormat, $current);

                    // if start date is bigger than 21 period, add array
                    $i++;

                    $dates[$i]['start'] = date($startFormat, $current);
                    $dates[$i]['end'] = date($endFormat, strtotime("+1 month", $current));


                }else{
                    $dates[$i]['end'] = date($endFormat, strtotime("+1 month", $current));
                }

            }else{

                $dates[$i]['start'] = date($startFormat, $current);
                $dates[$i]['end'] = date($endFormat, strtotime("+1 month", $current));;


            }

            $current = strtotime($step, $current);

            $i++;
        }



        return $dates;
}

$dates = dateRange('2015-01-12', '2015-05-23', "+1 month", "Y-m-d", '21');//increase by one month

由于

0 个答案:

没有答案