这里我在php日期有一些问题
代码
$calcdateloops = date("Y-m-01", strtotime(date('Y-m-d')." -1 year -6 Month")); //2015-01-01
$enddate = date('Y-m-d');
所以现在我尝试的是我需要将其分解为每3个月意味着一次的quatar
预期结果
1) 2015-01-01 - 2015-03-30 // first loop
2) 2015-04-01 - 2015-06-30
3) 2015-07-01 - 2015-09-30
.... so on upto the end date
有没有简单的方法来实现结果?
答案 0 :(得分:1)
课程DateTime和DateInterval是解决问题的有力工具,您无需关心每个月的天数。
// constructor accepts all the formats from strtotime function
$startdate = new DateTime('first day of this month - 18 months');
// without a value it returns current date
$enddate = new DateTime();
// all possible formats for DateInterval are in manual
// but basically you need to start with P indicating period
// and then number of days, months, seconds etc
$interval = new DateInterval('P3M');
do {
// without clone statement it will copy variables by reference
// meaning that all you variables points to the same object
$periodstart = clone $startdate;
$startdate->add($interval);
$periodend = clone $startdate;
// just subtract one day in order to prevent intersection of start
// and end dates from different periods
$periodend->sub(new DateInterval('P1D'));
echo 'start: ', $periodstart->format('Y-m-d'), ', ', 'end: ', $periodend->format('Y-m-d'), '<br>';
} while ($startdate < $enddate);