【发布时间】: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 做它?似乎这种类型的数据按摩可以用一个简单的脚本来完成。
-
抱歉解释不好。我修改了问题,请再次检查。谢谢