【问题标题】:How do I calculate billing cycle from given date in PHP?如何从 PHP 中的给定日期计算计费周期?
【发布时间】:2015-07-11 09:05:49
【问题描述】:

我想得到如下结果。

结算日基准点:21

正常计费周期为 2015-01-21 至 2015-02-20、2015-02-21 至 2015-03-20、2015-03-21 至 2015-04-20, ... ... . ..

作为结算周期的第一个月。

当客户注册开始日期(2015 年 1 月 13 日)和结束日期(2015 年 5 月 10 日)早于 2015 年 1 月 21 日(结算日 21)时, 我想将时间段设置为 2015-01-13 到 2015-01-20。

但如果客户注册开始日期 (2015-01-25) 和结束日期 (2015-05-25) 晚于 2015-01-21,我想将期间设为 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

谢谢

【问题讨论】:

  • 我们很乐意帮助回答您的问题,但似乎缺少很多信息。这些日期从何而来?所有这些日期都有数据源吗?你为什么用 PHP 做它?似乎这种类型的数据按摩可以用一个简单的脚本来完成。
  • 抱歉解释不好。我修改了问题,请再次检查。谢谢

标签: php date billing


【解决方案1】:

您可以为此使用DateTime

像这样:

$startDateString = '2015-01-10';
$startDate = new DateTime($startDateString);
$newDate = $startDate->add(new DateInterval('P1D')); // P1D adds one day

您可以创建一个for 循环来构造所需的数组作为结果。 请参阅DateInterval 上的docs 了解更多信息。

祝你好运!

【讨论】:

  • 我使用低于 PHP 5.3,所以我不能使用 DateInterval。感谢您的帮助。
  • @user1736363:无论哪种方式,在 DateTime 对象上使用 -&gt;modify('+1 day')
【解决方案2】:

我刚刚修改了我的函数如下,它可以工作,如果有人知道更好的代码,请告诉我。

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


        $i=0;
        $loopEnd = true;
        while( $current <= $last ){    

            if($first==$current){
                //Start Month

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

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


                    $dates[$i]['endDate'] = date($endFormat, $current);

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

                $nextDateEnd = $dates[$i]['endDate'];

            }else{


                $dates[$i]['startDate'] = date("Y-m-d", strtotime("+1 day", strtotime($nextDateEnd)));
                $dates[$i]['endDate'] = date($endFormat, strtotime("+1 month", strtotime($dates[$i]['startDate'])));;   


                if($lastDate < $dates[$i]['endDate']){
                    $dates[$i]['endDate'] = $lastDate;
                    $loopEnd = false;
                }

            }

            $nextDateStart = $dates[$i]['startDate'];
            $nextDateEnd = $dates[$i]['endDate'];

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

            $i++;
        }


        if($lastDate > date($endFormat, strtotime($lastDate)) and $loopEnd == true ){
            $i++;

            $dates[$i]['startDate'] = date("Y-m-d", strtotime("+1 day", strtotime($nextDateEnd)));
            $dates[$i]['endDate'] = $lastDate; 


        }

        return $dates;
}

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

结果:

Array
(
    [0] => Array
        (
        [start] => 2015-01-10
        [end] => 2015-01-20
    )

[1] => Array
    (
        [start] => 2015-01-21
        [end] => 2015-02-20
    )

[2] => Array
    (
        [start] => 2015-02-21
        [end] => 2015-03-20
    )

[3] => Array
    (
        [start] => 2015-03-21
        [end] => 2015-04-20
    )

[4] => Array
    (
        [start] => 2015-04-21
        [end] => 2015-05-11
    )

)

【讨论】:

    猜你喜欢
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2016-05-02
    • 1970-01-01
    相关资源
    最近更新 更多