如果你想分割更多的月份,你可以使用这个功能:
function getMonthRanges($start, $end) {
$timeStart = strtotime($start);
$timeEnd = strtotime($end);
$out = [];
$milestones[] = $timeStart;
$timeEndMonth = strtotime('first day of next month midnight', $timeStart);
while ($timeEndMonth < $timeEnd) {
$milestones[] = $timeEndMonth;
$timeEndMonth = strtotime('+1 month', $timeEndMonth);
}
$milestones[] = $timeEnd;
$count = count($milestones);
for ($i = 1; $i < $count; $i++) {
$out[] = [
'start' => $milestones[$i - 1], // Here you can apply your formatting (like "date('Y-m-d H:i:s', $milestones[$i-1])") if you don't won't want just timestamp
'end' => $milestones[$i] - 1
];
}
return $out;
}
使用:
$result = getMonthRanges('15-11-2014', '19-11-2014');
结果:
Array
(
[0] => Array
(
[start] => 1418594400
[end] => 1420063199
)
[1] => Array
(
[start] => 1420063200
[end] => 1422741599
)
[2] => Array
(
[start] => 1422741600
[end] => 1425160799
)
[3] => Array
(
[start] => 1425160800
[end] => 1426715999
)
)
人类可读的结果:
array_walk_recursive($result, 'applyFormat');
function applyFormat(&$item)
{
$item = date('Y-m-d H:i:s', $item);
}
Array
(
[0] => Array
(
[start] => 2014-12-15 00:00:00
[end] => 2014-12-31 23:59:59
)
[1] => Array
(
[start] => 2015-01-01 00:00:00
[end] => 2015-01-31 23:59:59
)
[2] => Array
(
[start] => 2015-02-01 00:00:00
[end] => 2015-02-28 23:59:59
)
[3] => Array
(
[start] => 2015-03-01 00:00:00
[end] => 2015-03-18 23:59:59
)
)